Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_events.cpp
index a2db2cbc22884f3a51c2c1355e42478075deb2d3..2c20eea1997d11b4b862e559ccc9fc0a37317396 100644 (file)
 #include <inttypes.h>
 #include <ctype.h>
 
-#include <common/sessiond-comm/sessiond-comm.h>
-#include <common/compat/string.h>
-#include <common/compat/getenv.h>
-#include <common/string-utils/string-utils.h>
-#include <common/utils.h>
+#include <common/sessiond-comm/sessiond-comm.hpp>
+#include <common/compat/string.hpp>
+#include <common/compat/getenv.hpp>
+#include <common/string-utils/string-utils.hpp>
+#include <common/utils.hpp>
 
 /* Mi dependancy */
-#include <common/mi-lttng.h>
+#include <common/mi-lttng.hpp>
 
-#include <lttng/domain-internal.h>
-#include <lttng/event-internal.h>
+#include <lttng/domain-internal.hpp>
+#include <lttng/event-internal.hpp>
 
-#include "../command.h"
-#include "../loglevel.h"
-#include "../uprobe.h"
+#include "../command.hpp"
+#include "../loglevel.hpp"
+#include "../uprobe.hpp"
 
 #if (LTTNG_SYMBOL_NAME_LEN == 256)
 #define LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API    "255"
 #endif
 
-static char *opt_event_list;
 static int opt_event_type;
 static const char *opt_loglevel;
 static int opt_loglevel_type;
@@ -254,7 +253,7 @@ char *print_exclusions(const struct lttng_dynamic_pointer_array *exclusions)
        }
 
        length += sizeof(preamble);
-       ret = (char *) zmalloc(length);
+       ret = calloc<char>(length);
        if (!ret) {
                return NULL;
        }
@@ -430,7 +429,7 @@ static void warn_on_truncated_exclusion_names(const struct lttng_dynamic_pointer
  * Enabling event using the lttng API.
  * Note: in case of error only the last error code will be return.
  */
-static int enable_events(char *session_name)
+static int enable_events(char *session_name, char *event_list)
 {
        int ret = CMD_SUCCESS, command_ret = CMD_SUCCESS;
        int error_holder = CMD_SUCCESS, warn = 0, error = 0, success = 1;
@@ -812,7 +811,7 @@ static int enable_events(char *session_name)
        }
 
        /* Strip event list */
-       event_name = strtok(opt_event_list, ",");
+       event_name = strtok(event_list, ",");
        while (event_name != NULL) {
                /* Copy name and type of the event */
                strncpy(ev->name, event_name, LTTNG_SYMBOL_NAME_LEN);
@@ -1222,6 +1221,8 @@ int cmd_enable_events(int argc, const char **argv)
        int opt, ret = CMD_SUCCESS, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       char *event_list = NULL;
+       const char *arg_event_list = NULL;
        const char *leftover = NULL;
        int event_type = -1;
 
@@ -1320,13 +1321,22 @@ int cmd_enable_events(int argc, const char **argv)
                }
        }
 
-       opt_event_list = (char*) poptGetArg(pc);
-       if (opt_event_list == NULL && opt_enable_all == 0) {
-               ERR("Missing event name(s).\n");
+       arg_event_list = poptGetArg(pc);
+       if (arg_event_list == NULL && opt_enable_all == 0) {
+               ERR("Missing event name(s).");
                ret = CMD_ERROR;
                goto end;
        }
 
+       if (opt_enable_all == 0) {
+               event_list = strdup(arg_event_list);
+               if (event_list == NULL) {
+                       PERROR("Failed to copy event name(s)");
+                       ret = CMD_ERROR;
+                       goto end;
+               }
+       }
+
        leftover = poptGetArg(pc);
        if (leftover) {
                ERR("Unknown argument: %s", leftover);
@@ -1345,7 +1355,7 @@ int cmd_enable_events(int argc, const char **argv)
                session_name = opt_session_name;
        }
 
-       command_ret = enable_events(session_name);
+       command_ret = enable_events(session_name, event_list);
        if (command_ret) {
                success = 0;
                goto mi_closing;
@@ -1387,6 +1397,8 @@ end:
                free(session_name);
        }
 
+       free(event_list);
+
        /* Overwrite ret if an error occurred in enable_events */
        ret = command_ret ? command_ret : ret;
 
This page took 0.026013 seconds and 4 git commands to generate.