File bsc#1211678-0003-Fix-libcrmcommon-Don-t-leak-memory-in-pcmk__cmdline_.patch of Package pacemaker.29826

From 904c5629c52165d6c1eb3068d293ecbf2880c5e7 Mon Sep 17 00:00:00 2001
From: Chris Lumens <clumens@redhat.com>
Date: Mon, 25 Jan 2021 11:07:50 -0500
Subject: [PATCH 3/6] Fix: libcrmcommon: Don't leak memory in
 pcmk__cmdline_preproc.

Converting the intermediate pointer array into a gchar ** with realloc
results in something that we don't know how to free.  Do we use free
because it was constructed with realloc, or do we use one of the g_free
functions because the contents of the array were allocated with
g_strdup?

Luckily, glib already solved this problem when they made g_strsplit (and
other functions that result in a gchar **).  So, just copy what they did
there.
---
 lib/common/cmdline.c | 18 ++++--------------
 1 file changed, 4 insertions(+), 14 deletions(-)

diff --git a/lib/common/cmdline.c b/lib/common/cmdline.c
index 66f197677..a21dfc028 100644
--- a/lib/common/cmdline.c
+++ b/lib/common/cmdline.c
@@ -1,5 +1,5 @@
 /*
- * Copyright 2019 the Pacemaker project contributors
+ * Copyright 2019-2021 the Pacemaker project contributors
  *
  * The version control history for this file may have further details.
  *
@@ -150,12 +150,11 @@ pcmk__add_arg_group(GOptionContext *context, const char *name,
 
 gchar **
 pcmk__cmdline_preproc(char **argv, const char *special) {
-    gchar **retval = NULL;
     GPtrArray *arr = NULL;
     bool saw_dash_dash = false;
 
     if (argv == NULL) {
-        return retval;
+        return NULL;
     }
 
     if (g_get_prgname() == NULL && argv && *argv) {
@@ -237,18 +236,9 @@ pcmk__cmdline_preproc(char **argv, const char *special) {
         }
     }
 
-    /* Convert the GPtrArray into a gchar **, which the command line parsing
-     * code knows how to deal with.  Then we can free the array (but not its
-     * contents).
-     */
-    retval = calloc(arr->len+1, sizeof(char *));
-    for (int i = 0; i < arr->len; i++) {
-        retval[i] = (gchar *) g_ptr_array_index(arr, i);
-    }
-
-    g_ptr_array_free(arr, FALSE);
+    g_ptr_array_add(arr, NULL);
 
-    return retval;
+    return (char **) g_ptr_array_free(arr, FALSE);
 }
 
 G_GNUC_PRINTF(3, 4)
-- 
2.35.3

openSUSE Build Service is sponsored by