File pacemaker#3394-0003-Low-libcrmcommon-NULL-check-strdup-in-pcmk__register.patch of Package pacemaker
From fad740e877849b49cb9956681ab54a5663ffeab0 Mon Sep 17 00:00:00 2001
From: Reid Wahl <nrwahl@protonmail.com>
Date: Wed, 20 Mar 2024 20:05:06 -0700
Subject: [PATCH 3/4] Low: libcrmcommon: NULL-check strdup() in
pcmk__register_format()
Dereferencing null pointer found by cppcheck.
Return ENOMEM instead of asserting since this is a library function that
returns a standard Pacemaker return code. However, nothing checks it,
and it would have segfaulted in the past, so it might be better to
assert here.
Signed-off-by: Reid Wahl <nrwahl@protonmail.com>
---
lib/common/output.c | 9 ++++++++-
1 file changed, 8 insertions(+), 1 deletion(-)
diff --git a/lib/common/output.c b/lib/common/output.c
index f3ec5cc12..e6caf5e19 100644
--- a/lib/common/output.c
+++ b/lib/common/output.c
@@ -128,8 +128,15 @@ pcmk__register_format(GOptionGroup *group, const char *name,
pcmk__output_factory_t create,
const GOptionEntry *options)
{
+ char *name_copy = NULL;
+
CRM_ASSERT(create != NULL && !pcmk__str_empty(name));
+ name_copy = strdup(name);
+ if (name_copy == NULL) {
+ return ENOMEM;
+ }
+
if (formatters == NULL) {
formatters = pcmk__strkey_table(free, NULL);
}
@@ -138,7 +145,7 @@ pcmk__register_format(GOptionGroup *group, const char *name,
g_option_group_add_entries(group, options);
}
- g_hash_table_insert(formatters, strdup(name), create);
+ g_hash_table_insert(formatters, name_copy, create);
return pcmk_rc_ok;
}
--
2.35.3