]> granicus.if.org Git - graphviz/commitdiff
use agxbbuf for dynamic 'Show_boxes' string construction
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Wed, 24 Aug 2022 04:02:54 +0000 (21:02 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 26 Aug 2022 14:54:21 +0000 (07:54 -0700)
We have some recent evidence¹ people are using this debug feature. This change
makes some allocations it does more robust (they now exit on failure) and
abbreviates construction in a stack buffer and then duplication into a heap
buffer into direct construction in a heap buffer.

¹ https://forum.graphviz.org/t/how-to-use-the-showboxes-attribute/1287

lib/common/postproc.c
lib/common/routespl.c
lib/common/splines.c

index bc712030dd6dfb963e93a3feb5ee98c928ab6e09..205a404aef90c854a8bae30c06add40a6d4f61d9 100644 (file)
@@ -8,6 +8,7 @@
  * Contributors: Details at https://graphviz.org
  *************************************************************************/
 
+#include <cgraph/agxbuf.h>
 #include <cgraph/unreachable.h>
 #include <common/render.h>
 #include <label/xlabels.h>
@@ -679,13 +680,13 @@ void gv_postprocess(Agraph_t * g, int allowTranslation)
        place_root_label(g, dimen);
 
     if (Show_boxes) {
-       char buf[BUFSIZ];
+       agxbuf buf = {0};
        if (Flip)
-           snprintf(buf, sizeof(buf), M2, Offset.x, Offset.y, Offset.x, Offset.y);
+           agxbprint(&buf, M2, Offset.x, Offset.y, Offset.x, Offset.y);
        else
-           snprintf(buf, sizeof(buf), M1, Offset.y, Offset.x, Offset.y, Offset.x,
+           agxbprint(&buf, M1, Offset.y, Offset.x, Offset.y, Offset.x,
                    -Offset.x, -Offset.y);
-       Show_boxes[0] = strdup(buf);
+       Show_boxes[0] = agxbdisown(&buf);
     }
 }
 
index cfd9d9475358e568df6b1ed0eb0b65a316ef2f63..ecb606fcf7aaf472ed547488d39f333cedfbcfaa 100644 (file)
@@ -9,7 +9,7 @@
  *************************************************************************/
 
 #include "config.h"
-
+#include <cgraph/agxbuf.h>
 #include <common/render.h>
 #include <math.h>
 #include <pathplan/pathplan.h>
@@ -33,15 +33,15 @@ static void printboxes(int boxn, boxf* boxes)
 {
     pointf ll, ur;
     int bi;
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + boxn;
 
     Show_boxes = ALLOC(newcnt+2,Show_boxes,char*);
     for (bi = 0; bi < boxn; bi++) {
        ll = boxes[bi].LL, ur = boxes[bi].UR;
-       snprintf(buf, sizeof(buf), "%d %d %d %d pathbox", (int)ll.x, (int)ll.y,
+       agxbuf buf = {0};
+       agxbprint(&buf, "%d %d %d %d pathbox", (int)ll.x, (int)ll.y,
                 (int)ur.x, (int)ur.y);
-       Show_boxes[bi+1+Show_cnt] = strdup (buf);
+       Show_boxes[bi + 1 + Show_cnt] = agxbdisown(&buf);
     }
     Show_cnt = newcnt;
     Show_boxes[Show_cnt+1] = NULL;
@@ -85,7 +85,6 @@ static void psprintpointf(pointf p)
 
 static void psprintspline(Ppolyline_t spl)
 {
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + spl.pn + 4;
     int li, i;
 
@@ -95,9 +94,10 @@ static void psprintspline(Ppolyline_t spl)
     Show_boxes[li++] = strdup ("%% spline");
     Show_boxes[li++] = strdup ("gsave 1 0 0 setrgbcolor newpath");
     for (i = 0; i < spl.pn; i++) {
-       snprintf(buf, sizeof(buf), "%f %f %s", spl.ps[i].x, spl.ps[i].y,
+       agxbuf buf = {0};
+       agxbprint(&buf, "%f %f %s", spl.ps[i].x, spl.ps[i].y,
          i == 0 ?  "moveto" : (i % 3 == 0 ? "curveto" : ""));
-       Show_boxes[li++] = strdup (buf);
+       Show_boxes[li++] = agxbdisown(&buf);
     }
     Show_boxes[li++] = strdup ("stroke grestore");
     Show_cnt = newcnt;
@@ -106,7 +106,6 @@ static void psprintspline(Ppolyline_t spl)
 
 static void psprintline(Ppolyline_t pl)
 {
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + pl.pn + 4;
     int i, li;
 
@@ -116,9 +115,10 @@ static void psprintline(Ppolyline_t pl)
     Show_boxes[li++] = strdup ("%% line");
     Show_boxes[li++] = strdup ("gsave 0 0 1 setrgbcolor newpath");
     for (i = 0; i < pl.pn; i++) {
-       snprintf(buf, sizeof(buf), "%f %f %s", pl.ps[i].x, pl.ps[i].y,
+       agxbuf buf = {0};
+       agxbprint(&buf, "%f %f %s", pl.ps[i].x, pl.ps[i].y,
                i == 0 ? "moveto" : "lineto");
-       Show_boxes[li++] = strdup (buf);
+       Show_boxes[li++] = agxbdisown(&buf);
     }
     Show_boxes[li++] = strdup ("stroke grestore");
     Show_cnt = newcnt;
@@ -127,7 +127,6 @@ static void psprintline(Ppolyline_t pl)
 
 static void psprintpoly(Ppoly_t p)
 {
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + p.pn + 3;
     point tl, hd;
     int bi, li;
@@ -144,9 +143,9 @@ static void psprintpoly(Ppoly_t p)
        hd.y = (int)p.ps[(bi+1) % p.pn].y;
        if (tl.x == hd.x && tl.y == hd.y) pfx = "%%";
        else pfx ="";
-       snprintf(buf, sizeof(buf), "%s%d %d %d %d makevec", pfx, tl.x, tl.y, hd.x,
-                hd.y);
-       Show_boxes[li++] = strdup (buf);
+       agxbuf buf = {0};
+       agxbprint(&buf, "%s%d %d %d %d makevec", pfx, tl.x, tl.y, hd.x, hd.y);
+       Show_boxes[li++] = agxbdisown(&buf);
     }
     Show_boxes[li++] = strdup ("grestore");
 
@@ -156,7 +155,6 @@ static void psprintpoly(Ppoly_t p)
 
 static void psprintboxes(int boxn, boxf* boxes)
 {
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + 5*boxn + 3;
     pointf ll, ur;
     int bi, li;
@@ -167,14 +165,15 @@ static void psprintboxes(int boxn, boxf* boxes)
     Show_boxes[li++] = strdup ("gsave 0 1 0 setrgbcolor");
     for (bi = 0; bi < boxn; bi++) {
        ll = boxes[bi].LL, ur = boxes[bi].UR;
-       snprintf(buf, sizeof(buf), "newpath\n%d %d moveto", (int)ll.x, (int)ll.y);
-       Show_boxes[li++] = strdup (buf);
-       snprintf(buf, sizeof(buf), "%d %d lineto", (int)ll.x, (int)ur.y);
-       Show_boxes[li++] = strdup (buf);
-       snprintf(buf, sizeof(buf), "%d %d lineto", (int)ur.x, (int)ur.y);
-       Show_boxes[li++] = strdup (buf);
-       snprintf(buf, sizeof(buf), "%d %d lineto", (int)ur.x, (int)ll.y);
-       Show_boxes[li++] = strdup (buf);
+       agxbuf buf = {0};
+       agxbprint(&buf, "newpath\n%d %d moveto", (int)ll.x, (int)ll.y);
+       Show_boxes[li++] = agxbdisown(&buf);
+       agxbprint(&buf, "%d %d lineto", (int)ll.x, (int)ur.y);
+       Show_boxes[li++] = agxbdisown(&buf);
+       agxbprint(&buf, "%d %d lineto", (int)ur.x, (int)ur.y);
+       Show_boxes[li++] = agxbdisown(&buf);
+       agxbprint(&buf, "%d %d lineto", (int)ur.x, (int)ll.y);
+       Show_boxes[li++] = agxbdisown(&buf);
        Show_boxes[li++] = strdup ("closepath stroke");
     }
     Show_boxes[li++] = strdup ("grestore");
index 98aa6016526698de9c7e493803b6243971844aa3..e81a3880f9bad359d706c76adabb54307e3e4086 100644 (file)
@@ -14,6 +14,7 @@
  */
 
 #include <math.h>
+#include <cgraph/agxbuf.h>
 #include <common/render.h>
 #include <cgraph/unreachable.h>
 #include <stdbool.h>
@@ -30,7 +31,6 @@ static int debugleveln(edge_t* e, int i)
 
 static void showPoints(pointf ps[], int pn)
 {
-    char buf[BUFSIZ];
     int newcnt = Show_cnt + pn + 3;
     int bi, li;
 
@@ -39,8 +39,9 @@ static void showPoints(pointf ps[], int pn)
     Show_boxes[li++] = strdup ("%% self list");
     Show_boxes[li++] = strdup ("dbgstart");
     for (bi = 0; bi < pn; bi++) {
-       snprintf(buf, sizeof(buf), "%.5g %.5g point", ps[bi].x, ps[bi].y);
-       Show_boxes[li++] = strdup (buf);
+       agxbuf buf = {0};
+       agxbprint(&buf, "%.5g %.5g point", ps[bi].x, ps[bi].y);
+       Show_boxes[li++] = agxbdisown(&buf);
     }
     Show_boxes[li++] = strdup ("grestore");