Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / enable_events.c
index 6889b63d73eb12cacc8db055c4bd0d2a0bcc1fd1..a833c23ad4a3fe7162867ca41803633276a541f8 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2011 - David Goulet <david.goulet@polymtl.ca>
+ * Copyright (C) 2011 EfficiOS Inc.
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License, version 2 only,
- * as published by the Free Software Foundation.
+ * SPDX-License-Identifier: GPL-2.0-only
  *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along
- * with this program; if not, write to the Free Software Foundation, Inc.,
- * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  */
 
 #define _LGPL_SOURCE
@@ -31,6 +21,7 @@
 #include <common/compat/getenv.h>
 #include <common/string-utils/string-utils.h>
 #include <common/utils.h>
+#include <common/path.h>
 
 #include <lttng/constant.h>
 /* Mi dependancy */
@@ -42,7 +33,6 @@
 #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;
@@ -143,7 +133,7 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        }
 
        /* Check for symbol */
-       if (isalpha(name[0])) {
+       if (isalpha(name[0]) || name[0] == '_') {
                match = sscanf(opt, "%" LTTNG_SYMBOL_NAME_LEN_SCANF_IS_A_BROKEN_API "s",
                        name);
                if (match == 1) {
@@ -160,8 +150,13 @@ static int parse_probe_opts(struct lttng_event *ev, char *opt)
        /* Check for address */
        match = sscanf(opt, "%" S_HEX_LEN_SCANF_IS_A_BROKEN_API "s", s_hex);
        if (match > 0) {
-               if (*s_hex == '\0') {
-                       ERR("Invalid probe address %s", s_hex);
+               /*
+                * Return an error if the first character of the tentative
+                * address is NULL or not a digit. It can be "0" if the address
+                * is in hexadecimal and can be 1 to 9 if it's in decimal.
+                */
+               if (*s_hex == '\0' || !isdigit(*s_hex)) {
+                       ERR("Invalid probe description %s", s_hex);
                        ret = CMD_ERROR;
                        goto end;
                }
@@ -966,7 +961,7 @@ static void warn_on_truncated_exclusion_names(char * const *exclusion_list,
  * 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;
@@ -1330,7 +1325,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);
@@ -1712,6 +1707,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;
 
@@ -1777,7 +1774,9 @@ int cmd_enable_events(int argc, const char **argv)
        }
 
        ret = print_missing_or_multiple_domains(
-               opt_kernel + opt_userspace + opt_jul + opt_log4j + opt_python);
+                       opt_kernel + opt_userspace + opt_jul + opt_log4j +
+                                       opt_python,
+                       true);
        if (ret) {
                ret = CMD_ERROR;
                goto end;
@@ -1808,13 +1807,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);
@@ -1833,7 +1841,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;
@@ -1875,6 +1883,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.025374 seconds and 4 git commands to generate.