Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / disable_channels.cpp
index df9eb13fac04a52f8334efbedab5ae2f38cd73b6..ff9bd91c8e818d91ebf0c42877474545befbb048 100644 (file)
@@ -19,7 +19,6 @@
 
 #include "../command.hpp"
 
-static char *opt_channels;
 static int opt_kernel;
 static char *opt_session_name;
 static int opt_userspace;
@@ -94,7 +93,7 @@ end:
 /*
  * Disabling channel using the lttng API.
  */
-static int disable_channels(char *session_name)
+static int disable_channels(char *session_name, char *channel_list)
 {
        int ret = CMD_SUCCESS, warn = 0, success;
 
@@ -133,7 +132,7 @@ static int disable_channels(char *session_name)
        }
 
        /* Strip channel list */
-       channel_name = strtok(opt_channels, ",");
+       channel_name = strtok(channel_list, ",");
        while (channel_name != NULL) {
                DBG("Disabling channel %s", channel_name);
 
@@ -207,6 +206,8 @@ int cmd_disable_channels(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *channel_list = NULL;
+       const char *arg_channel_list = NULL;
        const char *leftover = NULL;
 
        pc = poptGetContext(NULL, argc, argv, long_options, 0);
@@ -236,9 +237,16 @@ int cmd_disable_channels(int argc, const char **argv)
                goto end;
        }
 
-       opt_channels = (char*) poptGetArg(pc);
-       if (opt_channels == NULL) {
-               ERR("Missing channel name(s).\n");
+       arg_channel_list = poptGetArg(pc);
+       if (arg_channel_list == NULL) {
+               ERR("Missing channel name(s).");
+               ret = CMD_ERROR;
+               goto end;
+       }
+
+       channel_list = strdup(arg_channel_list);
+       if (channel_list == NULL) {
+               PERROR("Failed to copy channel name");
                ret = CMD_ERROR;
                goto end;
        }
@@ -285,7 +293,7 @@ int cmd_disable_channels(int argc, const char **argv)
                }
        }
 
-       command_ret = disable_channels(session_name);
+       command_ret = disable_channels(session_name, channel_list);
        if (command_ret) {
                success = 0;
        }
@@ -326,6 +334,8 @@ end:
                free(session_name);
        }
 
+       free(channel_list);
+
        /* Overwrite ret if an error occurred in disable_channels */
        ret = command_ret ? command_ret : ret;
 
This page took 0.024202 seconds and 4 git commands to generate.