From: Matthew Fernandez Date: Sat, 5 Nov 2022 03:22:19 +0000 (-0700) Subject: smyrna: fix memory leak in column construction X-Git-Tag: 7.0.2~18^2~1 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=18b45ed7337aceec22768131778eb2813b069520;p=graphviz smyrna: fix memory leak in column construction By using string views, we can avoid the need to duplicate the graph attribute value here. This allocation that was previously being lost no longer is made at all. Gitlab: fixes #2299 --- diff --git a/cmd/smyrna/tvnodes.c b/cmd/smyrna/tvnodes.c index 4402bc7bc..bca442f13 100644 --- a/cmd/smyrna/tvnodes.c +++ b/cmd/smyrna/tvnodes.c @@ -13,6 +13,8 @@ #include "viewport.h" #include "topviewfuncs.h" #include +#include +#include #include #include @@ -305,15 +307,14 @@ static GtkTreeView *update_tree(GtkTreeView * tree, grid * g) return tree; } -static void add_column(grid * g, char *name, bool editable, GType g_type) -{ - if (*name == '\0') +static void add_column(grid *g, strview_t name, bool editable, GType g_type) { + if (strview_str_eq(name, "")) return; assert(g->count >= 0); g->columns = gv_recalloc(g->columns, (size_t)g->count, (size_t)g->count + 1, sizeof(gridCol)); g->columns[g->count].editable = editable; - g->columns[g->count].name = gv_strdup(name); + g->columns[g->count].name = strview_str(name); g->columns[g->count].type = g_type; g->count++; } @@ -342,10 +343,6 @@ static grid *initGrid(void) static grid *update_columns(grid * g, char *str) { - /*free existing memory if necessary */ - char *a; - char *buf; - if (g) { if (g->flds != str) clearGrid(g); @@ -353,16 +350,15 @@ static grid *update_columns(grid * g, char *str) return g; } else g = initGrid(); - add_column(g, Name, false, G_TYPE_STRING); + add_column(g, strview(Name, '\0'), false, G_TYPE_STRING); if (!str) return g; g->flds = str; - buf = strdup (str); /* need to dup because str is actual graph attribute value */ - a = strtok(buf, ","); - add_column(g, a, true, G_TYPE_STRING); - while ((a = strtok(NULL, ","))) + for (tok_t t = tok(str, ","); !tok_end(&t); tok_next(&t)) { + strview_t a = tok_get(&t); add_column(g, a, true, G_TYPE_STRING); + } return g; }