6ca6e8
commit 6484ae5b8c4d4314f748e4d3c9a9baa5385e57c5
6ca6e8
Author: Carlos O'Donell <carlos@redhat.com>
6ca6e8
Date:   Fri Jan 28 15:14:29 2022 -0500
6ca6e8
6ca6e8
    malloc: Fix -Wuse-after-free warning in tst-mallocalign1 [BZ #26779]
6ca6e8
    
6ca6e8
    The test leaks bits from the freed pointer via the return value
6ca6e8
    in ret, and the compiler correctly identifies this issue.
6ca6e8
    We switch the test to use TEST_VERIFY and terminate the test
6ca6e8
    if any of the pointers return an unexpected alignment.
6ca6e8
    
6ca6e8
    This fixes another -Wuse-after-free error when compiling glibc
6ca6e8
    with gcc 12.
6ca6e8
    
6ca6e8
    Tested on x86_64 and i686 without regression.
6ca6e8
    
6ca6e8
    Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
6ca6e8
    (cherry picked from commit 3a7bed5f5a527dbd87412551f41e42e63aeef07a)
6ca6e8
6ca6e8
diff --git a/malloc/tst-mallocalign1.c b/malloc/tst-mallocalign1.c
6ca6e8
index 294e821afebd25ac..3e09ff30c4aa9f91 100644
6ca6e8
--- a/malloc/tst-mallocalign1.c
6ca6e8
+++ b/malloc/tst-mallocalign1.c
6ca6e8
@@ -20,6 +20,7 @@
6ca6e8
 #include <stdlib.h>
6ca6e8
 #include <inttypes.h>
6ca6e8
 #include <malloc-size.h>
6ca6e8
+#include <support/check.h>
6ca6e8
 
6ca6e8
 static void *
6ca6e8
 test (size_t s)
6ca6e8
@@ -31,41 +32,42 @@ test (size_t s)
6ca6e8
   return p;
6ca6e8
 }
6ca6e8
 
6ca6e8
+#define ALIGNED(p) (((uintptr_t )p & MALLOC_ALIGN_MASK) == 0)
6ca6e8
+
6ca6e8
 static int
6ca6e8
 do_test (void)
6ca6e8
 {
6ca6e8
   void *p;
6ca6e8
-  int ret = 0;
6ca6e8
 
6ca6e8
   p = test (2);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (8);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (13);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (16);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (23);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (43);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
   p = test (123);
6ca6e8
-  ret |= (uintptr_t) p & MALLOC_ALIGN_MASK;
6ca6e8
+  TEST_VERIFY (ALIGNED (p));
6ca6e8
   free (p);
6ca6e8
 
6ca6e8
-  return ret;
6ca6e8
+  return 0;
6ca6e8
 }
6ca6e8
 
6ca6e8
 #include <support/test-driver.c>