From db5dc6497e86b886a41d52ea13f0598ecaa9c296 Mon Sep 17 00:00:00 2001 From: Matthew Fernandez Date: Fri, 29 Jul 2022 17:24:05 -0700 Subject: [PATCH] replace NULL-hinted agxbuf usage with zero initialization 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. --- cmd/gvmap/make_map.c | 4 +--- cmd/smyrna/gui/menucallbacks.c | 3 +-- cmd/tools/graphml2gv.c | 4 ---- cmd/tools/gxl2gv.c | 5 +---- lib/common/htmllex.c | 2 +- lib/common/labels.c | 3 +-- lib/common/psusershape.c | 3 --- lib/common/utils.c | 11 +++-------- lib/gvc/gvplugin.c | 6 ------ lib/gvpr/actions.c | 4 ++-- lib/gvpr/compile.c | 7 ------- lib/gvpr/gvpr.c | 3 +-- lib/gvpr/parse.c | 3 +-- plugin/core/gvrender_core_dot.c | 5 +---- 14 files changed, 13 insertions(+), 50 deletions(-) diff --git a/cmd/gvmap/make_map.c b/cmd/gvmap/make_map.c index ea46c3c5f..abc112423 100644 --- a/cmd/gvmap/make_map.c +++ b/cmd/gvmap/make_map.c @@ -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; diff --git a/cmd/smyrna/gui/menucallbacks.c b/cmd/smyrna/gui/menucallbacks.c index 6bd5629ef..3c4846b09 100644 --- a/cmd/smyrna/gui/menucallbacks.c +++ b/cmd/smyrna/gui/menucallbacks.c @@ -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"); diff --git a/cmd/tools/graphml2gv.c b/cmd/tools/graphml2gv.c index ad53e5d97..e666bf24c 100644 --- a/cmd/tools/graphml2gv.c +++ b/cmd/tools/graphml2gv.c @@ -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; diff --git a/cmd/tools/gxl2gv.c b/cmd/tools/gxl2gv.c index 213b71314..794b09390 100644 --- a/cmd/tools/gxl2gv.c +++ b/cmd/tools/gxl2gv.c @@ -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; diff --git a/lib/common/htmllex.c b/lib/common/htmllex.c index d9a15b74d..704bc0c13 100644 --- a/lib/common/htmllex.c +++ b/lib/common/htmllex.c @@ -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; diff --git a/lib/common/labels.c b/lib/common/labels.c index 7c5dcf4ea..ae67f45a8 100644 --- a/lib/common/labels.c +++ b/lib/common/labels.c @@ -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++);) { diff --git a/lib/common/psusershape.c b/lib/common/psusershape.c index b232e90aa..17b986056 100644 --- a/lib/common/psusershape.c +++ b/lib/common/psusershape.c @@ -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) { diff --git a/lib/common/utils.c b/lib/common/utils.c index 0cfe3a788..6b28dcaa3 100644 --- a/lib/common/utils.c +++ b/lib/common/utils.c @@ -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); diff --git a/lib/gvc/gvplugin.c b/lib/gvc/gvplugin.c index 5108924b9..1a3faf0bf 100644 --- a/lib/gvc/gvplugin.c +++ b/lib/gvc/gvplugin.c @@ -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, ':'); diff --git a/lib/gvpr/actions.c b/lib/gvpr/actions.c index 63ebc4187..e380e0668 100644 --- a/lib/gvpr/actions.c +++ b/lib/gvpr/actions.c @@ -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') diff --git a/lib/gvpr/compile.c b/lib/gvpr/compile.c index 26d69e686..3c4527970 100644 --- a/lib/gvpr/compile.c +++ b/lib/gvpr/compile.c @@ -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); } diff --git a/lib/gvpr/gvpr.c b/lib/gvpr/gvpr.c index c867a298b..4af4a0b7d 100644 --- a/lib/gvpr/gvpr.c +++ b/lib/gvpr/gvpr.c @@ -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 */ diff --git a/lib/gvpr/parse.c b/lib/gvpr/parse.c index 2dc2592ea..ca7dc7ccd 100644 --- a/lib/gvpr/parse.c +++ b/lib/gvpr/parse.c @@ -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) { diff --git a/plugin/core/gvrender_core_dot.c b/plugin/core/gvrender_core_dot.c index 1991bfa25..bf34e3bba 100644 --- a/plugin/core/gvrender_core_dot.c +++ b/plugin/core/gvrender_core_dot.c @@ -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) { -- 2.50.1