]> git.ipfire.org Git - thirdparty/shadow.git/commitdiff
tests/unit/test_xasprintf.c: Fix use of volatile pointer
authorAlejandro Colomar <alx@kernel.org>
Fri, 28 Jun 2024 11:57:35 +0000 (13:57 +0200)
committerSerge Hallyn <serge@hallyn.com>
Fri, 28 Jun 2024 13:57:01 +0000 (08:57 -0500)
volatile needs to be casted away behind a [[gnu::noipa]] function, to
make that invisible to the compiler.  Otherwise, the compiler can see
that it is being discarded, and is free to abuse Undefined Behavior.

Closes: <https://github.com/shadow-maint/shadow/issues/1028>
Reported-by: Chris Hofstaedtler <zeha@debian.org>
Tested-by: Chris Hofstaedtler <zeha@debian.org>
Reviewed-by: Chris Hofstaedtler <zeha@debian.org>
Signed-off-by: Alejandro Colomar <alx@kernel.org>
tests/unit/test_xasprintf.c

index de22aca6d02d078cc02a8a4c8569dd2db90e8b3f..4c27d78a8a5c2e6991027373435a14330db7f9d2 100644 (file)
@@ -5,6 +5,7 @@
 
 
 #include <setjmp.h>
+#include <stdarg.h>
 #include <stddef.h>
 #include <stdlib.h>
 #include <string.h>
@@ -32,6 +33,10 @@ 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);
 
+[[gnu::noipa]]
+static int xasprintf_volatile(char *volatile *restrict s,
+    const char *restrict fmt, ...);
+
 static void test_xasprintf_exit(void **state);
 static void test_xasprintf_ok(void **state);
 
@@ -62,6 +67,18 @@ __wrap_exit(int status)
 }
 
 
+static int
+xasprintf_volatile(char *volatile *restrict s, const char *restrict fmt, ...)
+{
+       int      len;
+       va_list  ap;
+
+       va_start(ap, fmt);
+       len = xvasprintf((char **) s, fmt, ap);
+       va_end(ap);
+}
+
+
 static void
 test_xasprintf_exit(void **state)
 {
@@ -75,7 +92,7 @@ test_xasprintf_exit(void **state)
        switch (setjmp(jmpb)) {
        case 0:
                len = XASPRINTF_CALLED;
-               len = xasprintf(&p, "foo%s", "bar");
+               len = xasprintf_volatile(&p, "foo%s", "bar");
                assert_unreachable();
                break;
        case EXIT_CALLED: