Fix: lttng: poptGetArg doesn't provide string ownership
[lttng-tools.git] / src / bin / lttng / commands / clear.c
index 353c855c6f11aed6036f6a8f855035d31d7f8b54..82d938ff7e339623d7c9bd50fa7ff7def784aeef 100644 (file)
@@ -1,18 +1,8 @@
 /*
- * Copyright (C) 2019 - Jonathan Rajotte-Julien <jonathan.rajotte-julien@efficios.com>
+ * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
  *
- * 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
@@ -24,8 +14,7 @@
 #include <sys/types.h>
 #include <unistd.h>
 #include <stdbool.h>
-#include <lttng/clear.h>
-#include <lttng/clear-handle.h>
+#include <lttng/lttng.h>
 
 #include "../command.h"
 
@@ -167,8 +156,8 @@ int cmd_clear(int argc, const char **argv)
        int ret = CMD_SUCCESS , i, command_ret = CMD_SUCCESS, success = 1;
        static poptContext pc;
        char *session_name = NULL;
+       const char *arg_session_name = NULL;
        const char *leftover = NULL;
-
        struct lttng_session *sessions = NULL;
        int count;
        int found;
@@ -224,24 +213,22 @@ int cmd_clear(int argc, const char **argv)
        }
 
        if (!opt_clear_all) {
-               /*
-                * popt expects us to free this even if it returns a const char *.
-                * See https://www.mail-archive.com/popt-devel@rpm5.org/msg00193.html
-                * Force cast to char * allowing later freeing if necessary.
-                */
-               session_name = (char *) poptGetArg(pc);
-
-               if (!session_name) {
+               arg_session_name = poptGetArg(pc);
+               if (!arg_session_name) {
                        /* No session name specified, lookup default */
                        session_name = get_session_name();
+               } else {
+                       session_name = strdup(arg_session_name);
                        if (session_name == NULL) {
-                               command_ret = CMD_ERROR;
-                               success = 0;
-                               goto mi_closing;
+                               PERROR("Failed to copy session name");
                        }
                }
-       } else {
-               session_name = NULL;
+
+               if (session_name == NULL) {
+                       command_ret = CMD_ERROR;
+                       success = 0;
+                       goto mi_closing;
+               }
        }
 
        leftover = poptGetArg(pc);
This page took 0.024744 seconds and 4 git commands to generate.