From: erg <devnull@localhost>
Date: Thu, 28 Jul 2005 22:33:31 +0000 (+0000)
Subject: Fix code not to do malloc(0); short-circuit when component has only
X-Git-Tag: LAST_LIBGRAPH~32^2~7361
X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=c60c34696fbd3c48c6ae892e8972304a2ecce644;p=graphviz

Fix code not to do malloc(0); short-circuit when component has only
a single node
---

diff --git a/lib/neatogen/constrained_majorization.c b/lib/neatogen/constrained_majorization.c
index 9d948aaa8..6a0f05b15 100644
--- a/lib/neatogen/constrained_majorization.c
+++ b/lib/neatogen/constrained_majorization.c
@@ -141,11 +141,12 @@ stress_majorization_with_hierarchy(
 		if (dim==2) {
 			IMDS_given_dim(graph, n, y, x, Epsilon);
 		}
-	}	
+	}
 	else {
         initLayout(graph, n, dim, d_coords);
 		hierarchy_spread = compute_hierarchy(graph, n, abs_tol, relative_tol, NULL, &ordering, &levels, &num_levels);		
 	}
+    if (n == 1) return 0;
 
 	hierarchy_boundaries = N_GNEW(num_levels, float);
 
diff --git a/lib/neatogen/dijkstra.c b/lib/neatogen/dijkstra.c
index 29858cbbd..5d37ec0f3 100644
--- a/lib/neatogen/dijkstra.c
+++ b/lib/neatogen/dijkstra.c
@@ -80,24 +80,31 @@ static void heapify(heap * h, int i, int index[], Word dist[])
     }
 }
 
+#ifdef OBSOLETE
+/* originally, the code called mkHeap to allocate space, then
+ * initHeap to realloc the space. This is silly.
+ * Now we just call initHeap.
+ */
 static void mkHeap(heap * h, int size)
 {
     h->data = N_GNEW(size, int);
     h->heapSize = 0;
 }
+#endif
 
 static void freeHeap(heap * h)
 {
-    free(h->data);
+    if (h->data) free(h->data);
 }
 
 static void
 initHeap(heap * h, int startVertex, int index[], Word dist[], int n)
 {
     int i, count;
-    int j;			/* We cannot use an unsigned value in this loop */
+    int j;    /* We cannot use an unsigned value in this loop */
     /* h->data=(int*) realloc(h->data, (n-1)*sizeof(int)); */
-    h->data = N_GNEW(n - 1, int);
+    if (n == 1) h->data = NULL;
+    else h->data = N_GNEW(n - 1, int);
     h->heapSize = n - 1;
 
     for (count = 0, i = 0; i < n; i++)
@@ -156,7 +163,9 @@ void dijkstra(int vertex, vtx_data * graph, int n, DistType * dist)
     DistType closestDist, prevClosestDist = MAXINT;
     static int *index;
 
+#ifdef OBSOLETE
     mkHeap(&H, n);
+#endif
     index = (int *) realloc(index, n * sizeof(int));
 
     /* initial distances with edge weights: */
@@ -229,7 +238,9 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
     }
 
 
+#ifdef OBSOLETE
     mkHeap(&H, n);
+#endif
     index = (int *) realloc(index, n * sizeof(int));
 
     /* initial distances with edge weights: */
@@ -239,7 +250,8 @@ dijkstra_bounded(int vertex, vtx_data * graph, int n, DistType * dist,
     for (i = 1; i < graph[vertex].nedges; i++)
 	dist[graph[vertex].edges[i]] = (DistType) graph[vertex].ewgts[i];
 
-    initHeap(&H, vertex, index, dist, n);	/* again, TOO COSTLY (O(n)) to put all nodes in heap! */
+    /* again, TOO COSTLY (O(n)) to put all nodes in heap! */
+    initHeap(&H, vertex, index, dist, n); 
 
     while (num_found < num_visited_nodes
 	   && extractMax(&H, &closestVertex, index, dist)) {
@@ -356,7 +368,9 @@ void dijkstra_f(int vertex, vtx_data * graph, int n, float *dist)
     float closestDist;
     int *index;
 
+#ifdef OBSOLETE
     mkHeap(&H, n);
+#endif
     index = N_GNEW(n, int);
 
     /* initial distances with edge weights: */
diff --git a/lib/neatogen/stress.c b/lib/neatogen/stress.c
index a44c38a5f..cc2f1a85d 100644
--- a/lib/neatogen/stress.c
+++ b/lib/neatogen/stress.c
@@ -1583,6 +1583,7 @@ int stress_majorization_kD_mkernel(vtx_data * graph,	/* Input graph in sparse re
     } else {
 	havePinned = initLayout(graph, n, dim, d_coords);
     }
+    if (n == 1) return 0;
 
     if (Verbose) {
 	fprintf(stderr, ": %.2f sec\n", elapsed_sec());