From: Alejandro Colomar Date: Tue, 4 Nov 2025 12:37:26 +0000 (+0100) Subject: tests/unit/test_exit_if_null.c: Test through XMALLOC() instead of xaprintf() X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=refs%2Fheads%2Fmaster;p=thirdparty%2Fshadow.git tests/unit/test_exit_if_null.c: Test through XMALLOC() instead of xaprintf() Both are indirect tests for exit_if_null(), but through XMALLOC() we can test it more robustly, as we don't need to wrap vasprintf(3) to make it fail. It's trivial to make MALLOC(3) fail: pass a huge size. The tests with xaprintf() were failing on Nix. I suspect the compiler was inlining aggressively, and as a result, the interposition of vasprintf(3) in cmocka wasn't actually working. The approach with XMALLOC() seems to work on Nix, as we don't need to interpose malloc(3). We still need to interpose exit(3), but for some reason that works fine. Closes: Reported-by: Silvan Mosberger Tested-by: Silvan Mosberger Reviewed-by: Iker Pedrosa Signed-off-by: Alejandro Colomar --- diff --git a/tests/unit/Makefile.am b/tests/unit/Makefile.am index a0e1fad61..9a6387ebe 100644 --- a/tests/unit/Makefile.am +++ b/tests/unit/Makefile.am @@ -132,7 +132,7 @@ test_typetraits_LDADD = \ test_exit_if_null_SOURCES = \ ../../lib/exit_if_null.c \ ../../lib/shadowlog.c \ - ../../lib/string/sprintf/aprintf.c \ + ../../lib/alloc/malloc.c \ ../../lib/string/strcmp/streq.c \ test_exit_if_null.c \ $(NULL) @@ -140,7 +140,6 @@ test_exit_if_null_CFLAGS = \ $(AM_CFLAGS) \ $(NULL) test_exit_if_null_LDFLAGS = \ - -Wl,-wrap,vasprintf \ -Wl,-wrap,exit \ $(NULL) test_exit_if_null_LDADD = \ diff --git a/tests/unit/test_exit_if_null.c b/tests/unit/test_exit_if_null.c index 0dda1592a..4a66e870d 100644 --- a/tests/unit/test_exit_if_null.c +++ b/tests/unit/test_exit_if_null.c @@ -2,11 +2,10 @@ // SPDX-License-Identifier: BSD-3-Clause -#include "string/sprintf/aprintf.h" +#include "alloc/malloc.h" #include -#include -#include +#include #include #include @@ -16,10 +15,10 @@ #include // Required by #include +#include "sizeof.h" #include "string/strcmp/streq.h" -#define smock() _Generic(mock(), uintmax_t: (intmax_t) mock()) #define assert_unreachable() assert_true(0) #define EXIT_CALLED (42) @@ -28,8 +27,6 @@ static jmp_buf jmpb; -int __real_vasprintf(char **restrict p, const char *restrict fmt, va_list ap); -int __wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap); void __wrap_exit(int status); static void test_exit_if_null_exit(void **state); @@ -48,13 +45,6 @@ main(void) } -int -__wrap_vasprintf(char **restrict p, const char *restrict fmt, va_list ap) -{ - return smock() == -1 ? -1 : __real_vasprintf(p, fmt, ap); -} - - void __wrap_exit(int status) { @@ -67,14 +57,12 @@ test_exit_if_null_exit(void **state) { char *volatile p; - will_return(__wrap_vasprintf, -1); - p = "before"; switch (setjmp(jmpb)) { case 0: p = "called"; - p = xaprintf("foo%s", "bar"); + p = XMALLOC(SIZE_MAX, char); assert_unreachable(); break; case EXIT_CALLED: @@ -95,10 +83,15 @@ test_exit_if_null_ok(void **state) { char *p; - // Trick: vasprintf(3) will actually return the new string, not 0. - will_return(__wrap_vasprintf, 0); + static const char foo[] = "foo1bar"; - p = xaprintf("foo%d%s", 1, "bar"); + p = XMALLOC(countof(foo), char); + assert_true(p != NULL); + strcpy(p, foo); assert_string_equal(p, "foo1bar"); free(p); + + p = XMALLOC(0, char); + assert_true(p != NULL); + free(p); }