]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_exit_if_null.c: Test through XMALLOC() instead of xaprintf() master
authorAlejandro Colomar <alx@kernel.org>
Tue, 4 Nov 2025 12:37:26 +0000 (13:37 +0100)
committerAlejandro Colomar <foss+github@alejandro-colomar.es>
Thu, 6 Nov 2025 12:31:01 +0000 (13:31 +0100)
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: <https://github.com/shadow-maint/shadow/issues/1382>
Reported-by: Silvan Mosberger <github@infinisil.com>
Tested-by: Silvan Mosberger <github@infinisil.com>
Reviewed-by: Iker Pedrosa <ipedrosa@redhat.com>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
tests/unit/Makefile.am
tests/unit/test_exit_if_null.c

index a0e1fad61537dbaeceeafbcdba7ba90d208c0604..9a6387ebe2796a917d907be2140778e6494d8b2e 100644 (file)
@@ -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 = \
index 0dda1592ad0d8ce421f087d35deecd77ff97da9d..4a66e870da36195f87c562be8bf358981f414d7a 100644 (file)
@@ -2,11 +2,10 @@
 // SPDX-License-Identifier: BSD-3-Clause
 
 
-#include "string/sprintf/aprintf.h"
+#include "alloc/malloc.h"
 
 #include <setjmp.h>
-#include <stdarg.h>
-#include <stddef.h>
+#include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
 
 #include <stdint.h>  // Required by <cmocka.h>
 #include <cmocka.h>
 
+#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);
 }