From 954b494e39e17e36e05c1241d8a65fdfb4682acf Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Thu, 19 May 2022 22:27:49 -0700 Subject: [PATCH] gxl2gv: avoid large on-stack buffer MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 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 | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/cmd/tools/gxl2gv.c b/cmd/tools/gxl2gv.c index d7215d184..e916255b8 100644 --- a/cmd/tools/gxl2gv.c +++ b/cmd/tools/gxl2gv.c @@ -16,6 +16,7 @@ #include #include #include +#include #ifdef HAVE_EXPAT #include #include @@ -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); -- 2.40.0