ctdb-tools: Fix CID 1539212 - signed/unsigned issue
authorMartin Schwenke <mschwenke@ddn.com>
Sun, 30 Jul 2023 01:07:47 +0000 (11:07 +1000)
committerAmitay Isaacs <amitay@samba.org>
Mon, 7 Aug 2023 08:43:39 +0000 (08:43 +0000)
>>>     CID 1539212:  Control flow issues  (NO_EFFECT)
>>>     This greater-than-or-equal-to-zero comparison of an unsigned value is always true. "p >= 0UL".
216      while (p >= 0 && output[p] == '\n') {

This is a real problem in the unlikely event that the output contains
only newlines.

Fix the issue by using a pointer and add a test to cover this case.

BUG: https://bugzilla.samba.org/show_bug.cgi?id=15438

Signed-off-by: Martin Schwenke <mschwenke@ddn.com>
Reviewed-by: Amitay Isaacs <amitay@gmail.com>
ctdb/event/event_tool.c
ctdb/tests/UNIT/eventd/etc-ctdb/events/random/02.enabled.script
ctdb/tests/UNIT/eventd/eventd_008.sh

index e6d5261c217929828615031082dfabe00a32eda5..d6b7156c69bf96edf28fa50102156574e7c82182 100644 (file)
@@ -203,7 +203,6 @@ static void print_status_one(struct ctdb_event_script *script)
                const char *t = script->output == NULL ? "" : script->output;
                size_t len = strlen(t);
                char output[len+1];
-               size_t p;
                char *t1, *t2;
 
                strlcpy(output, t, sizeof(output));
@@ -212,10 +211,10 @@ static void print_status_one(struct ctdb_event_script *script)
                 * Strip trailing newlines, they are clutter and
                 * interfere with multi-line detection
                 */
-               p = len - 1;
-               while (p >= 0 && output[p] == '\n') {
-                       output[p] = '\0';
-                       p--;
+               t1 = output + len - 1;
+               while (t1 >= output && *t1 == '\n') {
+                       *t1 = '\0';
+                       t1--;
                }
 
                /* If the output is a single line then print it inline */
index 90df5218a9d9a3e5ae8989d9bc97fe8784a7fa4d..ace80fd8e56e7e88d8edb481ca7fe5c7079aee90 100755 (executable)
@@ -24,6 +24,14 @@ EOF
        printf 'No trailing newline'
        exit 0
        ;;
+"verbosenewlinesonly")
+       cat <<EOF
+
+
+
+EOF
+       exit 0
+       ;;
 "verbosefailure") echo "args: $*"; exit 1 ;;
 "verbosemultilinefailure")
        cat <<EOF
index c52caa0e25eb5e3b163673106c15c1907e918b88..bd0fc50009c9bd5c2bb275955edc201659d52841 100755 (executable)
@@ -56,6 +56,16 @@ ok <<EOF
 EOF
 simple_test status random verbosemultilinenonl
 
+ok_null
+simple_test run 10 random verbosenewlinesonly
+
+ok <<EOF
+01.disabled          DISABLED  
+02.enabled           OK         DURATION DATETIME
+  OUTPUT: 
+EOF
+simple_test status random verbosenewlinesonly
+
 required_result 8 <<EOF
 Event verbosemultilinefailure in random failed
 EOF