]> git.kaiwu.me - haproxy.git/commitdiff
BUG/MINOR: vars: fix missing string termination in get var on too long values
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 16:16:44 +0000 (17:16 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 20 Nov 2026 16:28:20 +0000 (17:28 +0100)
vars_parse_cli_get_var() builds the output for the CLI's "get var" in the
shared trash chunk using b_putchr(), which silently drops bytes once the
buffer is full. For a value long enough to exhaust trash mid-loop, the
trailing '>' and the terminating NUL were dropped, leaving trash.area
without any NUL within its bounds. cli_msg() only stores the pointer and the
string is later read with ist() (i.e. strlen) in cli_output_msg(), reading
past the buffer.

Now the loop stops before the last byte, and the terminating NUL is always
written within bounds, truncating the output if needed.

This patch should fix the issue #3592. It could be backported to all
supported versions.

src/vars.c

index 0b4d506af3bae0b8139ab2f35ca520dbedd1381c..29ca311abe1aa77743bfa05a0bcd1187bd09836f 100644 (file)
@@ -1817,15 +1817,17 @@ static int vars_parse_cli_get_var(char **args, char *payload, struct appctx *app
        } else {
                /* Display the displayable chars*. */
                b_putchr(&trash, '<');
-               for (i = 0; i < smp.data.u.str.data; i++) {
+               for (i = 0; i < smp.data.u.str.data && trash.data < trash.size - 1; i++) {
                        if (isprint((unsigned char)smp.data.u.str.area[i]))
                                b_putchr(&trash, smp.data.u.str.area[i]);
                        else
                                b_putchr(&trash, '.');
                }
-               b_putchr(&trash, '>');
-               b_putchr(&trash, '\n');
-               b_putchr(&trash, 0);
+               if (trash.data < trash.size - 1)
+                       b_putchr(&trash, '>');
+               if (trash.data < trash.size - 1)
+                       b_putchr(&trash, '\n');
+               trash.area[trash.data < trash.size ? trash.data : trash.size - 1] = 0;
        }
        return cli_msg(appctx, LOG_INFO, trash.area);
 }