|
|
23c78e |
From 477b7b679d58455dc38c2594b29a1ecfbe88e80c Mon Sep 17 00:00:00 2001
|
|
|
23c78e |
From: Chris Lumens <clumens@redhat.com>
|
|
|
23c78e |
Date: Mon, 2 Nov 2020 14:55:27 -0500
|
|
|
23c78e |
Subject: [PATCH 1/2] Fix: libcrmcommon: Prevent a segfault in
|
|
|
23c78e |
pcmk__cmdline_preproc.
|
|
|
23c78e |
|
|
|
23c78e |
The list of special single-character args is optional. The function
|
|
|
23c78e |
currently handles it being an empty string, but it should handle a NULL
|
|
|
23c78e |
as well.
|
|
|
23c78e |
---
|
|
|
23c78e |
lib/common/cmdline.c | 2 +-
|
|
|
23c78e |
1 file changed, 1 insertion(+), 1 deletion(-)
|
|
|
23c78e |
|
|
|
23c78e |
diff --git a/lib/common/cmdline.c b/lib/common/cmdline.c
|
|
|
23c78e |
index d66ccc7..66f1976 100644
|
|
|
23c78e |
--- a/lib/common/cmdline.c
|
|
|
23c78e |
+++ b/lib/common/cmdline.c
|
|
|
23c78e |
@@ -203,7 +203,7 @@ pcmk__cmdline_preproc(char **argv, const char *special) {
|
|
|
23c78e |
* glib does not. Grab both the argument and its value and
|
|
|
23c78e |
* separate them into a new argument.
|
|
|
23c78e |
*/
|
|
|
23c78e |
- if (strchr(special, *ch) != NULL) {
|
|
|
23c78e |
+ if (special != NULL && strchr(special, *ch) != NULL) {
|
|
|
23c78e |
/* The argument does not occur at the end of this string of
|
|
|
23c78e |
* arguments. Take everything through the end as its value.
|
|
|
23c78e |
*/
|
|
|
23c78e |
--
|
|
|
23c78e |
1.8.3.1
|
|
|
23c78e |
|
|
|
23c78e |
|
|
|
23c78e |
From d1f4a975fa783045254521f415f1899b34ee96e3 Mon Sep 17 00:00:00 2001
|
|
|
23c78e |
From: Chris Lumens <clumens@redhat.com>
|
|
|
23c78e |
Date: Mon, 2 Nov 2020 16:06:29 -0500
|
|
|
23c78e |
Subject: [PATCH 2/2] Test: libcrmcommon: Add unit tests for
|
|
|
23c78e |
pcmk__cmdline_preproc.
|
|
|
23c78e |
|
|
|
23c78e |
---
|
|
|
23c78e |
configure.ac | 1 +
|
|
|
23c78e |
lib/common/tests/Makefile.am | 2 +-
|
|
|
23c78e |
lib/common/tests/cmdline/Makefile.am | 29 ++++++
|
|
|
23c78e |
.../tests/cmdline/pcmk__cmdline_preproc_test.c | 102 +++++++++++++++++++++
|
|
|
23c78e |
4 files changed, 133 insertions(+), 1 deletion(-)
|
|
|
23c78e |
create mode 100644 lib/common/tests/cmdline/Makefile.am
|
|
|
23c78e |
create mode 100644 lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
|
|
|
23c78e |
|
|
|
23c78e |
diff --git a/configure.ac b/configure.ac
|
|
|
23c78e |
index 7ed4a30..36e85a9 100644
|
|
|
23c78e |
--- a/configure.ac
|
|
|
23c78e |
+++ b/configure.ac
|
|
|
23c78e |
@@ -2006,6 +2006,7 @@ AC_CONFIG_FILES(Makefile \
|
|
|
23c78e |
lib/pacemaker-cluster.pc \
|
|
|
23c78e |
lib/common/Makefile \
|
|
|
23c78e |
lib/common/tests/Makefile \
|
|
|
23c78e |
+ lib/common/tests/cmdline/Makefile \
|
|
|
23c78e |
lib/common/tests/flags/Makefile \
|
|
|
23c78e |
lib/common/tests/operations/Makefile \
|
|
|
23c78e |
lib/common/tests/strings/Makefile \
|
|
|
23c78e |
diff --git a/lib/common/tests/Makefile.am b/lib/common/tests/Makefile.am
|
|
|
23c78e |
index 33c45cb..f3eaeec 100644
|
|
|
23c78e |
--- a/lib/common/tests/Makefile.am
|
|
|
23c78e |
+++ b/lib/common/tests/Makefile.am
|
|
|
23c78e |
@@ -1 +1 @@
|
|
|
23c78e |
-SUBDIRS = flags operations strings utils
|
|
|
23c78e |
+SUBDIRS = cmdline flags operations strings utils
|
|
|
23c78e |
diff --git a/lib/common/tests/cmdline/Makefile.am b/lib/common/tests/cmdline/Makefile.am
|
|
|
23c78e |
new file mode 100644
|
|
|
23c78e |
index 0000000..e69ef21
|
|
|
23c78e |
--- /dev/null
|
|
|
23c78e |
+++ b/lib/common/tests/cmdline/Makefile.am
|
|
|
23c78e |
@@ -0,0 +1,29 @@
|
|
|
23c78e |
+#
|
|
|
23c78e |
+# Copyright 2020 the Pacemaker project contributors
|
|
|
23c78e |
+#
|
|
|
23c78e |
+# The version control history for this file may have further details.
|
|
|
23c78e |
+#
|
|
|
23c78e |
+# This source code is licensed under the GNU General Public License version 2
|
|
|
23c78e |
+# or later (GPLv2+) WITHOUT ANY WARRANTY.
|
|
|
23c78e |
+#
|
|
|
23c78e |
+AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_builddir)/include
|
|
|
23c78e |
+LDADD = $(top_builddir)/lib/common/libcrmcommon.la
|
|
|
23c78e |
+
|
|
|
23c78e |
+include $(top_srcdir)/mk/glib-tap.mk
|
|
|
23c78e |
+
|
|
|
23c78e |
+# Add each test program here. Each test should be written as a little standalone
|
|
|
23c78e |
+# program using the glib unit testing functions. See the documentation for more
|
|
|
23c78e |
+# information.
|
|
|
23c78e |
+#
|
|
|
23c78e |
+# https://developer.gnome.org/glib/unstable/glib-Testing.html
|
|
|
23c78e |
+#
|
|
|
23c78e |
+# Add "_test" to the end of all test program names to simplify .gitignore.
|
|
|
23c78e |
+test_programs = pcmk__cmdline_preproc_test
|
|
|
23c78e |
+
|
|
|
23c78e |
+# If any extra data needs to be added to the source distribution, add it to the
|
|
|
23c78e |
+# following list.
|
|
|
23c78e |
+dist_test_data =
|
|
|
23c78e |
+
|
|
|
23c78e |
+# If any extra data needs to be used by tests but should not be added to the
|
|
|
23c78e |
+# source distribution, add it to the following list.
|
|
|
23c78e |
+test_data =
|
|
|
23c78e |
diff --git a/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
|
|
|
23c78e |
new file mode 100644
|
|
|
23c78e |
index 0000000..e13c983
|
|
|
23c78e |
--- /dev/null
|
|
|
23c78e |
+++ b/lib/common/tests/cmdline/pcmk__cmdline_preproc_test.c
|
|
|
23c78e |
@@ -0,0 +1,102 @@
|
|
|
23c78e |
+/*
|
|
|
23c78e |
+ * Copyright 2020 the Pacemaker project contributors
|
|
|
23c78e |
+ *
|
|
|
23c78e |
+ * The version control history for this file may have further details.
|
|
|
23c78e |
+ *
|
|
|
23c78e |
+ * This source code is licensed under the GNU Lesser General Public License
|
|
|
23c78e |
+ * version 2.1 or later (LGPLv2.1+) WITHOUT ANY WARRANTY.
|
|
|
23c78e |
+ */
|
|
|
23c78e |
+
|
|
|
23c78e |
+#include <crm_internal.h>
|
|
|
23c78e |
+#include <crm/common/cmdline_internal.h>
|
|
|
23c78e |
+
|
|
|
23c78e |
+#define LISTS_EQ(a, b) { \
|
|
|
23c78e |
+ g_assert_cmpint(g_strv_length((gchar **) (a)), ==, g_strv_length((gchar **) (b))); \
|
|
|
23c78e |
+ for (int i = 0; i < g_strv_length((a)); i++) { \
|
|
|
23c78e |
+ g_assert_cmpstr((a)[i], ==, (b)[i]); \
|
|
|
23c78e |
+ } \
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+empty_input(void) {
|
|
|
23c78e |
+ g_assert_cmpint(pcmk__cmdline_preproc(NULL, "") == NULL, ==, TRUE);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+no_specials(void) {
|
|
|
23c78e |
+ const char *argv[] = { "-a", "-b", "-c", "-d", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "-a", "-b", "-c", "-d", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+
|
|
|
23c78e |
+ processed = pcmk__cmdline_preproc((char **) argv, "");
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+single_dash(void) {
|
|
|
23c78e |
+ const char *argv[] = { "-", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "-", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+double_dash(void) {
|
|
|
23c78e |
+ const char *argv[] = { "-a", "--", "-bc", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "-a", "--", "-bc", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+special_args(void) {
|
|
|
23c78e |
+ const char *argv[] = { "-aX", "-Fval", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "-a", "X", "-F", "val", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, "aF");
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+special_arg_at_end(void) {
|
|
|
23c78e |
+ const char *argv[] = { "-a", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "-a", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, "a");
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+static void
|
|
|
23c78e |
+long_arg(void) {
|
|
|
23c78e |
+ const char *argv[] = { "--blah=foo", NULL };
|
|
|
23c78e |
+ const gchar *expected[] = { "--blah=foo", NULL };
|
|
|
23c78e |
+
|
|
|
23c78e |
+ gchar **processed = pcmk__cmdline_preproc((char **) argv, NULL);
|
|
|
23c78e |
+ LISTS_EQ(processed, expected);
|
|
|
23c78e |
+ g_strfreev(processed);
|
|
|
23c78e |
+}
|
|
|
23c78e |
+
|
|
|
23c78e |
+int
|
|
|
23c78e |
+main(int argc, char **argv)
|
|
|
23c78e |
+{
|
|
|
23c78e |
+ g_test_init(&argc, &argv, NULL);
|
|
|
23c78e |
+
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/empty_input", empty_input);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/no_specials", no_specials);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/single_dash", single_dash);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/double_dash", double_dash);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/special_args", special_args);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/special_arg_at_end", special_arg_at_end);
|
|
|
23c78e |
+ g_test_add_func("/common/cmdline/preproc/long_arg", long_arg);
|
|
|
23c78e |
+ return g_test_run();
|
|
|
23c78e |
+}
|
|
|
23c78e |
--
|
|
|
23c78e |
1.8.3.1
|
|
|
23c78e |
|