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