From: Carlos O'Donell Date: Fri, 28 Jan 2022 20:14:29 +0000 (-0500) Subject: malloc: Fix -Wuse-after-free warning in tst-mallocalign1 [BZ #26779] X-Git-Tag: glibc-2.35~21 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=3a7bed5f5a527dbd87412551f41e42e63aeef07a;p=thirdparty%2Fglibc.git malloc: Fix -Wuse-after-free warning in tst-mallocalign1 [BZ #26779] The test leaks bits from the freed pointer via the return value in ret, and the compiler correctly identifies this issue. We switch the test to use TEST_VERIFY and terminate the test if any of the pointers return an unexpected alignment. This fixes another -Wuse-after-free error when compiling glibc with gcc 12. Tested on x86_64 and i686 without regression. Reviewed-by: Siddhesh Poyarekar --- diff --git a/malloc/tst-mallocalign1.c b/malloc/tst-mallocalign1.c index 8bfd50c468d..3116748e7ec 100644 --- a/malloc/tst-mallocalign1.c +++ b/malloc/tst-mallocalign1.c @@ -20,6 +20,7 @@ #include #include #include +#include static void * test (size_t s) @@ -31,41 +32,42 @@ test (size_t s) return p; } +#define ALIGNED(p) (((uintptr_t )p & MALLOC_ALIGN_MASK) == 0) + static int do_test (void) { void *p; - int ret = 0; p = test (2); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (8); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (13); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (16); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (23); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (43); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); p = test (123); - ret |= (uintptr_t) p & MALLOC_ALIGN_MASK; + TEST_VERIFY (ALIGNED (p)); free (p); - return ret; + return 0; } #include