]> granicus.if.org Git - graphviz/commitdiff
remove intermediate buffer in dot_polygon
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 24 Feb 2021 04:22:38 +0000 (20:22 -0800)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Tue, 2 Mar 2021 06:05:11 +0000 (22:05 -0800)
This buffer was only used to calculate how many bytes were necessary to
allocate. The semantics of snprintf allow us to achieve this without the buffer.

cmd/gvmap/make_map.c

index 6bf1fcbd366226ee34b9195e7621b46afe90ad0d..996efad6c87739d12862abcf0b8166f68f1475cf 100644 (file)
@@ -641,28 +641,28 @@ static void dot_polygon(char **sbuff, int *len, int *len_max, int np, float *xp,
                        int fill, int close, char *cstring){
   int i;
   int ret = 0;
-  char buf[10000];
   char swidth[10000];
   size_t len_swidth;
 
   if (np > 0){
     /* figure out the size needed */
     if (fill >= 0){/* poly*/
-      ret += snprintf(buf, sizeof(buf), " c %zu -%s C %zu -%s P %d ", strlen(cstring), cstring, strlen(cstring), cstring, np);
+      ret += snprintf(NULL, 0, " c %zu -%s C %zu -%s P %d ", strlen(cstring), cstring, strlen(cstring), cstring, np);
     } else {/* line*/
       assert(line_width >= 0);
       if (line_width > 0){
        sprintf(swidth,"%f",line_width);
        len_swidth = strlen(swidth);
        sprintf(swidth,"S %zu -setlinewidth(%f)",len_swidth+14, line_width);
-       ret += snprintf(buf, sizeof(buf), " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np);
+       ret += snprintf(NULL, 0, " c %zu -%s %s L %d ", strlen(cstring), cstring, swidth, np);
       } else {
-       ret += snprintf(buf, sizeof(buf), " c %zu -%s L %d ", strlen(cstring), cstring, np);
+       ret += snprintf(NULL, 0, " c %zu -%s L %d ", strlen(cstring), cstring, np);
       }
     }
     for (i = 0; i < np; i++){
-      ret += sprintf(buf, " %f %f",xp[i], yp[i]);
+      ret += snprintf(NULL, 0, " %f %f",xp[i], yp[i]);
     }
+    ++ret; // account for terminating '\0'
 
     if (*len + ret > *len_max - 1){
       *len_max = *len_max + MAX(100, 0.2*(*len_max)) + ret;