File bsc#1206761-0001-High-tools-Fix-a-segfault-in-error-handling-in-crm_r.patch of Package pacemaker.32051

From 074c56362fc4f73953d2fb436bd90cef7a51d229 Mon Sep 17 00:00:00 2001
From: Chris Lumens <clumens@redhat.com>
Date: Mon, 18 Jul 2022 13:55:00 -0400
Subject: [PATCH] High: tools: Fix a segfault in error handling in
 crm_resource.

First, we need to allocate the right amount of memory for printing error
messages.  Add space for the initial "non-option..." string and another
space for the NULL terminator on a string vector.

I think what happened here is that the "non-option..." string is not
terminated with a newline, making it appear as if it is the same string
as the first extra command line parameter string.  However, it does
actually take up its own slot in the string vector and so needs a slot
allocated for it.

So while I'm at it, add the newline back in.  It appears to have been
incorrectly removed when crm_resource was converted to using GError
instead of CMD_ERR.

Fixes T522
See: clbz#5496
---
 tools/crm_resource.c | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/tools/crm_resource.c b/tools/crm_resource.c
index 175fc446b..451531eca 100644
--- a/tools/crm_resource.c
+++ b/tools/crm_resource.c
@@ -1625,20 +1625,26 @@ main(int argc, char **argv)
 
         CRM_ASSERT(len > 0);
 
-        strv = calloc(len, sizeof(char *));
-        strv[0] = strdup("non-option ARGV-elements:");
+        /* Add 1 for the strv[0] string below, and add another 1 for the NULL
+         * at the end of the array so g_strjoinv knows when to stop.
+         */
+        strv = calloc(len+2, sizeof(char *));
+        strv[0] = strdup("non-option ARGV-elements:\n");
 
         for (gchar **s = options.remainder; *s; s++) {
             strv[i] = crm_strdup_printf("[%d of %d] %s\n", i, len, *s);
             i++;
         }
 
+        strv[i] = NULL;
+
         exit_code = CRM_EX_USAGE;
         msg = g_strjoinv("", strv);
         g_set_error(&error, PCMK__EXITC_ERROR, exit_code, "%s", msg);
         g_free(msg);
 
-        for(i = 0; i < len; i++) {
+        /* Don't try to free the last element, which is just NULL. */
+        for(i = 0; i < len+1; i++) {
             free(strv[i]);
         }
         free(strv);
-- 
2.35.3

openSUSE Build Service is sponsored by