]> granicus.if.org Git - graphviz/commitdiff
sfdpgen spring_electrical_embedding_slow: use an agxbuf instead of 'sprintf'
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 2 Jan 2023 02:39:27 +0000 (18:39 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 14 Jan 2023 17:56:12 +0000 (09:56 -0800)
This leads to less manual memory management and fewer problems to think about.

Gitlab: #1950

lib/sfdpgen/spring_electrical.c

index 2e850891f95dd8bcb031c3d8405ef9f23cbda5db..fb64f158026fb6a2d5129bf3dd8e3bc386078ce0 100644 (file)
@@ -9,6 +9,7 @@
  *************************************************************************/
 
 #include "config.h"
+#include <cgraph/agxbuf.h>
 #include <cgraph/alloc.h>
 #include <cgraph/bitarray.h>
 #include <sparse/SparseMatrix.h>
@@ -692,10 +693,10 @@ static void spring_electrical_embedding_slow(int dim, SparseMatrix A0, spring_el
 #ifdef DEBUG_0
   {
     FILE *f;
-    char fname[10000];
-    strcpy(fname,"/tmp/graph_layout_0_");
-    sprintf(&(fname[strlen(fname)]), "%d",n);
-    f = fopen(fname,"w");
+    agxbuf fname = {0};
+    agxbprint(&fname, "/tmp/graph_layout_0_%d", n);
+    f = fopen(agxbuse(&fname), "w");
+    agxbfree(&fname);
     export_embedding(f, dim, A, x, NULL);
     fclose(f);
   }
@@ -777,10 +778,10 @@ static void spring_electrical_embedding_slow(int dim, SparseMatrix A0, spring_el
 #ifdef DEBUG_PRINT_0
   {
     FILE *f;
-    char fname[10000];
-    strcpy(fname,"/tmp/graph_layout");
-    sprintf(&(fname[strlen(fname)]), "%d",n);
-    f = fopen(fname,"w");
+    agxbuf fname = {0};
+    agxbprint(&fname, "/tmp/graph_layout%d", n);
+    f = fopen(agxbuse(&fname), "w");
+    agxbfree(&fname);
     export_embedding(f, dim, A, x, NULL);
     fclose(f);
   }