]> git.ipfire.org Git - thirdparty/kmod.git/commitdiff
libkmod: strbuf_pushchars: handle pushing empty string gracefully
authorMartin Wilck <martin_wilck@gmx.de>
Fri, 15 Nov 2024 22:26:26 +0000 (23:26 +0100)
committerLucas De Marchi <lucas.de.marchi@gmail.com>
Fri, 29 Nov 2024 15:15:21 +0000 (09:15 -0600)
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 <martin_wilck@gmx.de>
Reviewed-by: Emil Velikov <emil.l.velikov@gmail.com>
Link: https://github.com/kmod-project/kmod/pull/257
Signed-off-by: Lucas De Marchi <lucas.de.marchi@gmail.com>
shared/strbuf.c
testsuite/test-strbuf.c

index dad5da525cb1cecce0cc57865fdcf8de265f1c29..0dd7a5b0dc9522a9a62bf4764f2cc117386adab5 100644 (file)
@@ -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;
 
index 9f2dcf8f5c776361330955fc0c4d8a9e4a9d7f7e..50c75d92446cb70b1d9995ad9a8787d86251ebc2 100644 (file)
@@ -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)
 {