]> granicus.if.org Git - graphviz/commitdiff
smyrna: duplicate names when creating new grid columns
authorMatthew Fernandez <matthew.fernandez@gmail.com>
Sat, 5 Nov 2022 02:54:27 +0000 (19:54 -0700)
committerMatthew Fernandez <matthew.fernandez@gmail.com>
Thu, 10 Nov 2022 03:50:53 +0000 (19:50 -0800)
This is a step towards solving a memory leak. Note that this involved rewriting
some string comparisons because the originating pointer is no longer stored in
the column’s `name`. It is not clear to me how the previous code was valid
because, e.g., it was comparing by pointer to the `Visible` constant to detect
when a column was called “Visible”. That is, nothing actually assigns a column
name to the address of `Visible`.

Gitlab: #2299

cmd/smyrna/tvnodes.c

index 3e90d8a6d6976e55613f798efa9f972165699a5d..ed131f2d51869c687e2af005a3ba8fed29e7f1bd 100644 (file)
@@ -14,6 +14,7 @@
 #include "topviewfuncs.h"
 #include <cgraph/alloc.h>
 #include <stdbool.h>
+#include <string.h>
 
 typedef struct {
     GType type;
@@ -205,13 +206,13 @@ static void populate_data(Agraph_t * g, grid * grid)
 
        for (id = 0; id < grid->count; id++) {
            cp = grid->columns[id];
-           if (cp->name == ID) continue;
+           if (strcmp(cp->name, ID) == 0) continue;
 
-           if (cp->name == Name)
+           if (strcmp(cp->name, Name) == 0)
                bf = agnameof(v);
            else
                bf = agget(v, cp->name);
-           if ((!bf) && (cp->name != Visible))
+           if (!bf && strcmp(cp->name, Visible) != 0)
                continue;
 
            g_value_init(&value, cp->type);
@@ -224,7 +225,7 @@ static void populate_data(Agraph_t * g, grid * grid)
                    else
                        g_value_set_boolean(&value, 0);
                } else {
-                   if (cp->name == Visible)
+                   if (strcmp(cp->name, Visible) == 0)
                        g_value_set_boolean(&value, 1);
                }
                break;
@@ -314,7 +315,7 @@ static void add_column(grid * g, char *name, bool editable, GType g_type)
                              sizeof(gridCol*));
     g->columns[g->count] = gv_alloc(sizeof(gridCol));
     g->columns[g->count]->editable = editable;
-    g->columns[g->count]->name = name;
+    g->columns[g->count]->name = gv_strdup(name);
     g->columns[g->count]->type = g_type;
     g->count++;
 }
@@ -323,6 +324,7 @@ static void clearGrid(grid * g)
 {
     int id;
     for (id = 0; id < g->count; id++) {
+       free(g->columns[id]->name);
        free(g->columns[id]);
     }
     free(g->columns);