]> granicus.if.org Git - graphviz/commitdiff
gml2gv: simplify string construction during parsing
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 12 May 2022 04:16:08 +0000 (21:16 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Sun, 22 May 2022 18:46:16 +0000 (11:46 -0700)
This also removes the possibility of unchecked allocation failures in this code.

cmd/tools/gmlscan.l

index 4002a3ba769fd50aaffeff4d205cc7b3826b9995..fa955b2a5cb47dccd312a1aa4bd40633ae477b48 100644 (file)
 %option noinput
 
 %{
+#include <assert.h>
+#include <cgraph/alloc.h>
 #include <stdlib.h>
+#include <string.h>
 #include <gml2gv.h>
 #include <gmlparse.h>
 #include "config.h"
@@ -37,36 +40,32 @@ void initgmlscan(FILE *ifile)
 #endif
 
 /* buffer for arbitrary length strings (longer than BUFSIZ) */
-static char     *Sbuf,*Sptr,*Send;
-static void beginstr(void) 
-{
-    if (Sbuf == NULL) {
-       Sbuf = malloc(BUFSIZ);
-       Send = Sbuf + BUFSIZ;
-    }
-    Sptr = Sbuf;
-    *Sptr = 0;
+static char *Sbuf;
+
+static void beginstr(void) {
+  assert(Sbuf == NULL && "leaking memory");
+  Sbuf = gv_strdup("");
 }
 
-static void addstr(char *src) 
-{
-    char    c;
-    if (Sptr > Sbuf) Sptr--;
-    do {
-       do {c = *Sptr++ = *src++;} while (c && (Sptr < Send));
-       if (c) {
-           long    sz = Send - Sbuf;
-           long    off = Sptr - Sbuf;
-           sz *= 2;
-           Sbuf = realloc(Sbuf,sz);
-           Send = Sbuf + sz;
-           Sptr = Sbuf + off;
-       }
-    } while (c);
+static void addstr(const char *src) {
+  assert(Sbuf != NULL && "missing beginstr()");
+
+  // enlarge the buffer to make room for the suffix
+  {
+    size_t old_size = strlen(Sbuf) + 1;
+    size_t new_size = old_size + strlen(src);
+    Sbuf = gv_realloc(Sbuf, old_size, new_size);
+  }
+
+  strcat(Sbuf, src);
 }
 
 static void endstr(void) {
-    gmllval.str = strdup(Sbuf);
+  assert(Sbuf != NULL && "missing beginstr()");
+
+  // take ownership of the Sbuf backing memory
+  gmllval.str = Sbuf;
+  Sbuf = NULL;
 }
 
 %}