The interface pointer in globals was used to store an address in .rodata or
in on the heap. This makes the handling of the allocation/deallocation
hard. At the same time, nothing checked for allocations problems. An
allocation error could cause an hard-to-debug problem later when trying to
calculate the ifindex.

Adjust the code to always allocate the string as an heap object to simplify
the handling.

Fixes: 2e3e312f7757 ("alfred: add vis")
Signed-off-by: Sven Eckelmann <[email protected]>
---
 vis/vis.c | 12 +++++++++++-
 vis/vis.h |  2 +-
 2 files changed, 12 insertions(+), 2 deletions(-)

diff --git a/vis/vis.c b/vis/vis.c
index d635798..97c6093 100644
--- a/vis/vis.c
+++ b/vis/vis.c
@@ -1058,10 +1058,15 @@ static struct globals *vis_init(int argc, char *argv[])
        memset(globals, 0, sizeof(*globals));
 
        globals->opmode = OPMODE_CLIENT;
-       globals->interface = "bat0";
        globals->vis_format = FORMAT_DOT;
        globals->unix_path = ALFRED_SOCK_PATH_DEFAULT;
 
+       globals->interface = strdup("bat0");
+       if (!globals->interface) {
+               perror("strdup");
+               return NULL;
+       }
+
        while ((opt = getopt_long(argc, argv, "shf:i:vu:", long_options,
                                  &opt_ind)) != -1) {
                switch (opt) {
@@ -1081,7 +1086,12 @@ static struct globals *vis_init(int argc, char *argv[])
                        }
                        break;
                case 'i':
+                       free(globals->interface);
                        globals->interface = strdup(optarg);
+                       if (!globals->interface) {
+                               perror("strdup");
+                               return NULL;
+                       }
                        break;
                case 'u':
                        globals->unix_path = optarg;
diff --git a/vis/vis.h b/vis/vis.h
index e257c45..d3486f9 100644
--- a/vis/vis.h
+++ b/vis/vis.h
@@ -73,7 +73,7 @@ struct vis_list_entry {
         (vis_data)->entries_n * sizeof(struct vis_entry))
 
 struct globals {
-       const char *interface;
+       char *interface;
        enum opmode opmode;
        enum vis_format vis_format;
        uint8_t buf[65536];

-- 
2.47.3

Reply via email to