]> granicus.if.org Git - graphviz/commitdiff
replace NULL-hinted agxbuf usage with zero initialization
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 30 Jul 2022 00:24:05 +0000 (17:24 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Mon, 8 Aug 2022 14:59:46 +0000 (07:59 -0700)
This leads to easy to read code and fewer things for the caller to think about.
I think this should probably become the default pattern for agxbuf usage for the
cases where the data you are printing is unpredictable or you are unsure.

14 files changed:
cmd/gvmap/make_map.c
cmd/smyrna/gui/menucallbacks.c
cmd/tools/graphml2gv.c
cmd/tools/gxl2gv.c
lib/common/htmllex.c
lib/common/labels.c
lib/common/psusershape.c
lib/common/utils.c
lib/gvc/gvplugin.c
lib/gvpr/actions.c
lib/gvpr/compile.c
lib/gvpr/gvpr.c
lib/gvpr/parse.c
plugin/core/gvrender_core_dot.c

index ea46c3c5f029d822b3c0366fb2ecf30ba8ab9f44..abc112423ab7aa58491d6b424e1cf81d4d5f6630 100644 (file)
@@ -317,9 +317,7 @@ void plot_dot_map(Agraph_t* gr, int n, int dim, double *x, SparseMatrix polys,
                   const char* opacity, SparseMatrix A, FILE* f) {
   /* if graph object exist, we just modify some attributes, otherwise we dump the whole graph */
   bool plot_polyQ = true;
-  agxbuf sbuff;
-
-  agxbinit(&sbuff, 0, NULL);
+  agxbuf sbuff = {0};
 
   if (!r || !g || !b) plot_polyQ = false;
 
index 6bd5629efa632c5218846c754a97f67338ee2732..3c4846b092442a0f5b4d2f51372445df85a37f02 100644 (file)
@@ -386,13 +386,12 @@ void on_gvprbuttonsave_clicked(GtkWidget * widget, gpointer user_data)
     (void)user_data;
 
     FILE *output_file = NULL;
-    agxbuf xbuf;
+    agxbuf xbuf = {0};
     GtkTextBuffer *gtkbuf;     /*GTK buffer from glade GUI */
     char *bf2;
     GtkTextIter startit;
     GtkTextIter endit;
 
-    agxbinit(&xbuf, SMALLBUF, NULL);
     /*file name should be returned in xbuf */
     if (savefiledlg(0, NULL, &xbuf)) {
        output_file = fopen(agxbuse(&xbuf), "w");
index ad53e5d97995fcc9d314f6e1b2505dc9cf5dd60d..e666bf24ce8c951c6823589b12e772d6b85ebf87 100644 (file)
@@ -31,7 +31,6 @@
 #define XML_STATUS_ERROR 0
 #endif
 
-#define SMALLBUF       1000
 #define NAMEBUF                100
 
 #define GRAPHML_ATTR   "_graphml_"
@@ -146,9 +145,6 @@ static Dtdisc_t nameDisc = {
 static userdata_t *genUserdata(char* dfltname)
 {
     userdata_t *user = gv_alloc(sizeof(*user));
-    agxbinit(&(user->xml_attr_name), NAMEBUF, 0);
-    agxbinit(&(user->xml_attr_value), SMALLBUF, 0);
-    agxbinit(&(user->composite_buffer), SMALLBUF, 0);
     user->listen = FALSE;
     user->elements = (gv_stack_t){0};
     user->closedElementType = TAG_NONE;
index 213b7131430794918241d438a63e3170ba3f507b..794b09390a5eae18e053c7050966a8c4209c3fbf 100644 (file)
@@ -151,12 +151,9 @@ static Dtdisc_t nameDisc = {
 
 static userdata_t *genUserdata(void)
 {
-    userdata_t *user = malloc(sizeof(userdata_t));
+    userdata_t *user = calloc(1, sizeof(userdata_t));
     if (user == NULL)
        return NULL;
-    agxbinit(&(user->xml_attr_name), NAMEBUF, 0);
-    agxbinit(&(user->xml_attr_value), SMALLBUF, 0);
-    agxbinit(&(user->composite_buffer), SMALLBUF, 0);
     user->listen = FALSE;
     user->elements = (gv_stack_t){0};
     user->closedElementType = TAG_NONE;
index d9a15b74da0dbaea7b374d937302a5fdd250d466..704bc0c13d0c794e31e46105242a12a3d6611b11 100644 (file)
@@ -760,7 +760,7 @@ int initHTMLlexer(char *src, agxbuf * xb, htmlenv_t *env)
 {
 #ifdef HAVE_EXPAT
     state.xb = xb;
-    agxbinit (&state.lb, SMALLBUF, NULL);
+    state.lb = (agxbuf){0};
     state.ptr = src;
     state.mode = 0;
     state.warn = 0;
index 7c5dcf4ea75110f6e96ca2266fd10de903a167dc..ae67f45a84612a2ff0a91bd3d56809b0bb654634 100644 (file)
@@ -295,7 +295,6 @@ static char *strdup_and_subst_obj0 (char *str, void *obj, int escBackslash)
     int isEdge = 0;
     textlabel_t *tl;
     port pt;
-    agxbuf buf;
 
     /* prepare substitution strings */
     switch (agobjkind(obj)) {
@@ -337,7 +336,7 @@ static char *strdup_and_subst_obj0 (char *str, void *obj, int escBackslash)
     }
 
     /* allocate a dynamic buffer that we will use to construct the result */
-    agxbinit(&buf, 0, NULL);
+    agxbuf buf = {0};
 
     /* assemble new string */
     for (s = str; (c = *s++);) {
index b232e90aa630eb19ace8de5f389270f2b45f77ed..17b986056f2e33f846b87b8787ed91f2d67b3229 100644 (file)
@@ -294,9 +294,6 @@ char *ps_string(char *ins, int chset)
        }
     }
 
-    if (xb.buf == NULL)
-        agxbinit (&xb, 0, NULL);
-
     agxbputc (&xb, LPAREN);
     s = base;
     while (*s) {
index 0cfe3a7880047479cf788ccefd8f6de0d58d8217..6b28dcaa311702501d8b2103dcea362e415cf1a9 100644 (file)
@@ -1427,7 +1427,6 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
     static graph_t* lastg;
     static bool warned;
     char*  ns;
-    agxbuf xb;
     unsigned char c;
     unsigned int v;
 
@@ -1439,7 +1438,7 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
        warned = false;
     }
 
-    agxbinit(&xb, 0, NULL);
+    agxbuf xb = {0};
 
     while ((c = *(unsigned char*)s++)) {
         if (c < 0xC0)
@@ -1512,11 +1511,9 @@ char* htmlEntityUTF8 (char* s, graph_t* g)
 char* latin1ToUTF8 (char* s)
 {
     char*  ns;
-    agxbuf xb;
+    agxbuf xb = {0};
     unsigned int  v;
 
-    agxbinit(&xb, 0, NULL);
-
     /* Values are either a byte (<= 256) or come from htmlEntity, whose
      * values are all less than 0x07FF, so we need at most 3 bytes.
      */
@@ -1551,12 +1548,10 @@ char*
 utf8ToLatin1 (char* s)
 {
     char*  ns;
-    agxbuf xb;
+    agxbuf xb = {0};
     unsigned char c;
     unsigned char outc;
 
-    agxbinit(&xb, 0, NULL);
-
     while ((c = *(unsigned char*)s++)) {
        if (c < 0x7F)
            agxbputc(&xb, (char)c);
index 5108924b9dfd625f2880f27ecb35fa7dc3e2ed20..1a3faf0bf10d7cf5f2e7a8d8b3e552a3596ead41 100644 (file)
@@ -337,7 +337,6 @@ gvplugin_available_t *gvplugin_load(GVC_t * gvc, api_t api, const char *str)
  */
 char *gvplugin_list(GVC_t * gvc, api_t api, const char *str)
 {
-    static int first = 1;
     const gvplugin_available_t *pnext, *plugin;
     char *bp;
     bool new = true;
@@ -347,11 +346,6 @@ char *gvplugin_list(GVC_t * gvc, api_t api, const char *str)
     if (!str)
         return NULL;
 
-    if (first) {
-        agxbinit(&xb, 0, 0);
-        first = 0;
-    }
-
     /* does str have a :path modifier? */
     const strview_t strv = strview(str, ':');
 
index 63ebc41878272acd59dc25d4a737cc04dd769d49..e380e0668d4e99b24c3abdd8482fae0c72a0425d 100644 (file)
@@ -710,14 +710,14 @@ char *readLine(Expr_t * ex, int fd)
 {
     Sfio_t *sp;
     int c;
-    agxbuf tmps;
     char *line;
 
     if (fd < 0 || fd >= elementsof(ex->file) || !((sp = ex->file[fd]))) {
        exerror("readL: %d: invalid descriptor", fd);
        return "";
     }
-    agxbinit(&tmps, 0, NULL);
+
+    agxbuf tmps = {0};
     while ((c = sfgetc(sp)) > 0 && c != '\n')
        agxbputc(&tmps, (char)c);
     if (c == '\n')
index 26d69e686280d87766bdbc140ee45d96936d3ef8..3c4527970651ee9c892b97acd6b81c22b1c1a619 100644 (file)
@@ -289,7 +289,6 @@ static Agobj_t *deref(Expr_t * pgm, Exnode_t * x, Exref_t * ref,
                    constant.value.integer);
        if (!ptr) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("null reference %s in expression %s.%s",
                  ref->symbol->name, ref->symbol->name, deparse(pgm, x, &xb));
            agxbfree(&xb);
@@ -1520,7 +1519,6 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
        objp = deref(pgm, node, ref, 0, state);
        if (!objp) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("null reference in expression %s", deparse(pgm, node, &xb));
            agxbfree(&xb);
        }
@@ -1568,7 +1566,6 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
        objp = state->curobj;
        if (!objp) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("current object $ not defined as reference for %s",
                  deparse(pgm, node, &xb));
            agxbfree(&xb);
@@ -1578,7 +1575,6 @@ getval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref,
     if (objp) {
        if (lookup(pgm, objp, sym, &v, state)) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("in expression %s", deparse(pgm, node, &xb));
            agxbfree(&xb);
            v.integer = 0;
@@ -1615,7 +1611,6 @@ setval(Expr_t * pgm, Exnode_t * x, Exid_t * sym, Exref_t * ref,
        objp = deref(pgm, x, ref, 0, state);
        if (!objp) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("in expression %s.%s", ref->symbol->name, deparse(pgm, x, &xb));
            agxbfree(&xb);
            return -1;
@@ -1669,7 +1664,6 @@ setval(Expr_t * pgm, Exnode_t * x, Exid_t * sym, Exref_t * ref,
        objp = state->curobj;
        if (!objp) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("current object $ undefined in expression %s",
                  deparse(pgm, x, &xb));
            agxbfree(&xb);
@@ -1859,7 +1853,6 @@ refval(Expr_t * pgm, Exnode_t * node, Exid_t * sym, Exref_t * ref)
     } else {
        if (!typeChkExp(ref, sym)) {
            agxbuf xb = {0};
-           agxbinit(&xb, 0, NULL);
            exerror("type error using %s", deparse(pgm, node, &xb));
            agxbfree(&xb);
        }
index c867a298b2f09cc14643b7b1de069a405cda2acf..4af4a0b7d634a39ac2d17158097c539ce3dd9b1e 100644 (file)
@@ -211,7 +211,6 @@ static char *resolve(char *arg, int Verbose)
     char *cp;
     char c;
     char *fname = 0;
-    agxbuf fp;
     char *pathp = NULL;
     size_t sz;
 
@@ -233,7 +232,7 @@ static char *resolve(char *arg, int Verbose)
        path = DFLT_GVPRPATH;
     if (Verbose)
        fprintf (stderr, "PATH: %s\n", path);
-    agxbinit(&fp, 0, NULL);
+    agxbuf fp = {0};
 
     while (*path && !fname) {
        if (*path == LISTSEP) { /* skip colons */
index 2dc2592ea959789ddd0ff0aba3cb7d83b7b31c51..ca7dc7ccd99a2c61239017b356e0c07143d92a61 100644 (file)
@@ -346,8 +346,7 @@ parseCase(Sfio_t * str, char **guard, int *gline, char **action,
 {
     case_t kind;
 
-    agxbuf buf;
-    agxbinit(&buf, 0, NULL);
+    agxbuf buf = {0};
 
     kind = parseKind(str);
     switch (kind) {
index 1991bfa25fef1226e08326e0abdf4d9d9fe278bb..bf34e3bbacebf4fec8ae1b73c45d9e97516a0a05 100644 (file)
@@ -252,10 +252,7 @@ static void xdot_style (GVJ_t *job)
  */
 static void put_escaping_backslashes(Agobj_t* n, Agsym_t *sym, const char *value)
 {
-    agxbuf buf;
-
-    /* create a temporary buffer */
-    agxbinit(&buf, 0, NULL);
+    agxbuf buf = {0};
 
     /* print the given string to the buffer, escaping as we go */
     for (; *value != '\0'; ++value) {