From fa97856d85675b213a98e8d202c0124c401d3ec4 Mon Sep 17 00:00:00 2001 From: Shawn Bohrer Date: Wed, 25 Jun 2025 17:52:18 -0500 Subject: [PATCH] Correctly handle vsnprintf truncation MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit From the vsnprintf man page: ``` The functions snprintf() and vsnprintf() do not write more than size bytes (including the terminating null byte ('\0')). If the output was truncated due to this limit, then the return value is the number of characters (excluding the terminating null byte) which would have been written to the final string if enough space had been available. Thus, a re‐ turn value of size or more means that the output was truncated. (See also below under CAVEATS.) ``` Without this fix performing an ipset list where `n == length` would result in a rust String that was terminated with a null byte, and this would fail to parse returning an error. For example: ``` called `Result::unwrap()` on an `Err` value: InvalidOutput("91.99.252.18\0") ``` --- wrapper.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/wrapper.c b/wrapper.c index 4607e8d..e95077d 100644 --- a/wrapper.c +++ b/wrapper.c @@ -23,14 +23,14 @@ int print_out(struct ipset_session *session, void *p, const char *fmt, ...) { free(data); return n; } - else if (n <= length) { + else if (n < length) { ipset_out(p, data, n, length); running = 0; length = n; } else { - length = n; + length = n + 1; free(data); } } while (running); return length; -} \ No newline at end of file +}