From: Martin Wilck Date: Fri, 15 Nov 2024 22:26:26 +0000 (+0100) Subject: libkmod: strbuf_pushchars: handle pushing empty string gracefully X-Git-Tag: v34~43 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=5f154ec21e0081daf8a02e3b6b1bf3836bb05801;p=thirdparty%2Fkmod.git libkmod: strbuf_pushchars: handle pushing empty string gracefully If buf->bytes, buf->used, and len are all 0, buf_grow() will do nothing, and memcpy() willbe called with a NULL first argument. This will cause an error because the function is annotated with __nonnull(1, 2). Fixes: e62d8c7 ("strbuf: make strbuf_pushchars() a little less dumb") Signed-off-by: Martin Wilck Reviewed-by: Emil Velikov Link: https://github.com/kmod-project/kmod/pull/257 Signed-off-by: Lucas De Marchi --- diff --git a/shared/strbuf.c b/shared/strbuf.c index dad5da52..0dd7a5b0 100644 --- a/shared/strbuf.c +++ b/shared/strbuf.c @@ -100,6 +100,9 @@ size_t strbuf_pushmem(struct strbuf *buf, const char *src, size_t sz) assert(src != NULL); assert(buf != NULL); + if (sz == 0) + return 0; + if (!strbuf_reserve_extra(buf, sz)) return 0; diff --git a/testsuite/test-strbuf.c b/testsuite/test-strbuf.c index 9f2dcf8f..50c75d92 100644 --- a/testsuite/test-strbuf.c +++ b/testsuite/test-strbuf.c @@ -202,8 +202,7 @@ static int test_strbuf_pushmem(const struct test *t) return 0; } -DEFINE_TEST(test_strbuf_pushmem, .description = "test strbuf_reserve", - .expected_fail = true); +DEFINE_TEST(test_strbuf_pushmem, .description = "test strbuf_reserve"); static int test_strbuf_used(const struct test *t) {