]> granicus.if.org Git - graphviz/commitdiff
gxl2gv: avoid large on-stack buffer
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Fri, 20 May 2022 05:27:49 +0000 (22:27 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 22 May 2022 23:40:16 +0000 (16:40 -0700)
MSVC warns:

  C:\GitLab-Runner\builds\graphviz\graphviz\cmd\tools\gxl2gv.c(694): warning
    C6262: Function uses '20020' bytes of stack:  exceeds /analyze:stacksize
    '16384'.  Consider moving some data to heap.
    [C:\GitLab-Runner\builds\graphviz\graphviz\cmd\tools\gxl2gv.vcxproj]

So this function attempts to allocate ~20KB of stack data. Most of this is due
to the `buf` array. Using this much stack can cause crashes in constrained
environments. The buffer in question is used to read XML data in chunks, so we
can instead use the C standard library’s constant `BUFSIZ` that tells us the
optimal chunk size with which to perform I/O.

This change avoids the described problem as well as being a performance
optimization.

cmd/tools/gxl2gv.c

index d7215d1847b911be5c89cbd3b6f77eec7810bc7f..e916255b86773f539a299d39d31c72db975a949f 100644 (file)
@@ -16,6 +16,7 @@
 #include    <cgraph/likely.h>
 #include    <cgraph/stack.h>
 #include    <common/memory.h>
+#include    <stdio.h>
 #ifdef HAVE_EXPAT
 #include    <expat.h>
 #include    <ctype.h>
@@ -27,7 +28,6 @@
 #define XML_STATUS_ERROR 0
 #endif
 
-#define BUFSIZE                20000
 #define SMALLBUF       1000
 #define NAMEBUF                100
 
@@ -693,7 +693,7 @@ static void characterDataHandler(void *userData, const char *s, int length)
 
 Agraph_t *gxl_to_gv(FILE * gxlFile)
 {
-    char buf[BUFSIZE];
+    char buf[BUFSIZ];
     int done;
     userdata_t *udata = genUserdata();
     XML_Parser parser = XML_ParserCreate(NULL);