From 6e57238bf915f8c6cfa0a2497c7311e760cae148 Mon Sep 17 00:00:00 2001 From: Alejandro Colomar Date: Fri, 28 Jun 2024 13:57:35 +0200 Subject: [PATCH] tests/unit/test_xasprintf.c: Fix use of volatile pointer 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: Reported-by: Chris Hofstaedtler Tested-by: Chris Hofstaedtler Reviewed-by: Chris Hofstaedtler Signed-off-by: Alejandro Colomar --- tests/unit/test_xasprintf.c | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/tests/unit/test_xasprintf.c b/tests/unit/test_xasprintf.c index de22aca6d..4c27d78a8 100644 --- a/tests/unit/test_xasprintf.c +++ b/tests/unit/test_xasprintf.c @@ -5,6 +5,7 @@ #include +#include #include #include #include @@ -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: -- 2.47.3