6ca6e8
commit 88b3228d9f6322b035fa89bb34f82d93b4190d48
6ca6e8
Author: Martin Sebor <msebor@redhat.com>
6ca6e8
Date:   Tue Jan 25 15:39:38 2022 -0700
6ca6e8
6ca6e8
    Avoid -Wuse-after-free in tests [BZ #26779].
6ca6e8
    
6ca6e8
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
6ca6e8
    (cherry picked from commit c094c232eb3246154265bb035182f92fe1b17ab8)
6ca6e8
6ca6e8
diff --git a/malloc/tst-malloc-backtrace.c b/malloc/tst-malloc-backtrace.c
6ca6e8
index 4eb42e7f32f706e9..2ae3fb11efc245cb 100644
6ca6e8
--- a/malloc/tst-malloc-backtrace.c
6ca6e8
+++ b/malloc/tst-malloc-backtrace.c
6ca6e8
@@ -20,6 +20,7 @@
6ca6e8
 #include <stdlib.h>
6ca6e8
 
6ca6e8
 #include <support/support.h>
6ca6e8
+#include <libc-diag.h>
6ca6e8
 
6ca6e8
 #define SIZE 4096
6ca6e8
 
6ca6e8
@@ -29,7 +30,15 @@ __attribute__((noinline))
6ca6e8
 call_free (void *ptr)
6ca6e8
 {
6ca6e8
   free (ptr);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a valid warning about using a pointer made indeterminate
6ca6e8
+     by a prior call to free().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
   *(size_t *)(ptr - sizeof (size_t)) = 1;
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
 }
6ca6e8
 
6ca6e8
 int
6ca6e8
diff --git a/malloc/tst-malloc-check.c b/malloc/tst-malloc-check.c
6ca6e8
index 6650d09cf6cdde75..c5c9254a9e8936b0 100644
6ca6e8
--- a/malloc/tst-malloc-check.c
6ca6e8
+++ b/malloc/tst-malloc-check.c
6ca6e8
@@ -87,7 +87,15 @@ do_test (void)
6ca6e8
     merror ("errno is not set correctly.");
6ca6e8
   DIAG_POP_NEEDS_COMMENT;
6ca6e8
 
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a valid warning about using a pointer made indeterminate
6ca6e8
+     by a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
   free (p);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
 
6ca6e8
   p = malloc (512);
6ca6e8
   if (p == NULL)
6ca6e8
@@ -105,7 +113,15 @@ do_test (void)
6ca6e8
     merror ("errno is not set correctly.");
6ca6e8
   DIAG_POP_NEEDS_COMMENT;
6ca6e8
 
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a valid warning about using a pointer made indeterminate
6ca6e8
+     by a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
   free (p);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
   free (q);
6ca6e8
 
6ca6e8
   return errors != 0;
6ca6e8
diff --git a/malloc/tst-malloc-too-large.c b/malloc/tst-malloc-too-large.c
6ca6e8
index a4349a9b4c506dfc..328b4a2a4fd72cf4 100644
6ca6e8
--- a/malloc/tst-malloc-too-large.c
6ca6e8
+++ b/malloc/tst-malloc-too-large.c
6ca6e8
@@ -95,7 +95,15 @@ test_large_allocations (size_t size)
6ca6e8
   DIAG_POP_NEEDS_COMMENT;
6ca6e8
 #endif
6ca6e8
   TEST_VERIFY (errno == ENOMEM);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a warning about using a pointer made indeterminate by
6ca6e8
+     a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
   free (ptr_to_realloc);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
 
6ca6e8
   for (size_t nmemb = 1; nmemb <= 8; nmemb *= 2)
6ca6e8
     if ((size % nmemb) == 0)
6ca6e8
@@ -113,14 +121,30 @@ test_large_allocations (size_t size)
6ca6e8
         test_setup ();
6ca6e8
         TEST_VERIFY (reallocarray (ptr_to_realloc, nmemb, size / nmemb) == NULL);
6ca6e8
         TEST_VERIFY (errno == ENOMEM);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a warning about using a pointer made indeterminate by
6ca6e8
+     a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
         free (ptr_to_realloc);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
 
6ca6e8
         ptr_to_realloc = malloc (16);
6ca6e8
         TEST_VERIFY_EXIT (ptr_to_realloc != NULL);
6ca6e8
         test_setup ();
6ca6e8
         TEST_VERIFY (reallocarray (ptr_to_realloc, size / nmemb, nmemb) == NULL);
6ca6e8
         TEST_VERIFY (errno == ENOMEM);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a warning about using a pointer made indeterminate by
6ca6e8
+     a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
         free (ptr_to_realloc);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
       }
6ca6e8
     else
6ca6e8
       break;
6ca6e8
diff --git a/malloc/tst-obstack.c b/malloc/tst-obstack.c
6ca6e8
index ee1385d0f764e368..7d700c4f9a86b676 100644
6ca6e8
--- a/malloc/tst-obstack.c
6ca6e8
+++ b/malloc/tst-obstack.c
6ca6e8
@@ -21,8 +21,8 @@ verbose_malloc (size_t size)
6ca6e8
 static void
6ca6e8
 verbose_free (void *buf)
6ca6e8
 {
6ca6e8
-  free (buf);
6ca6e8
   printf ("free (%p)\n", buf);
6ca6e8
+  free (buf);
6ca6e8
 }
6ca6e8
 
6ca6e8
 static int
6ca6e8
diff --git a/malloc/tst-realloc.c b/malloc/tst-realloc.c
6ca6e8
index c89ac07e192d70eb..80711beab1257ed5 100644
6ca6e8
--- a/malloc/tst-realloc.c
6ca6e8
+++ b/malloc/tst-realloc.c
6ca6e8
@@ -138,8 +138,16 @@ do_test (void)
6ca6e8
   if (ok == 0)
6ca6e8
     merror ("first 16 bytes were not correct after failed realloc");
6ca6e8
 
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  /* Ignore a valid warning about using a pointer made indeterminate
6ca6e8
+     by a prior call to realloc().  */
6ca6e8
+  DIAG_IGNORE_NEEDS_COMMENT (12, "-Wuse-after-free");
6ca6e8
+#endif
6ca6e8
   /* realloc (p, 0) frees p (C89) and returns NULL (glibc).  */
6ca6e8
   p = realloc (p, 0);
6ca6e8
+#if __GNUC_PREREQ (12, 0)
6ca6e8
+  DIAG_POP_NEEDS_COMMENT;
6ca6e8
+#endif
6ca6e8
   if (p != NULL)
6ca6e8
     merror ("realloc (p, 0) returned non-NULL.");
6ca6e8
 
6ca6e8
diff --git a/support/tst-support-open-dev-null-range.c b/support/tst-support-open-dev-null-range.c
6ca6e8
index 8e29def1ce780629..80c97e51586bf6ce 100644
6ca6e8
--- a/support/tst-support-open-dev-null-range.c
6ca6e8
+++ b/support/tst-support-open-dev-null-range.c
6ca6e8
@@ -39,10 +39,11 @@ check_path (int fd)
6ca6e8
   char file_path[PATH_MAX];
6ca6e8
   ssize_t file_path_length
6ca6e8
     = readlink (proc_fd_path, file_path, sizeof (file_path));
6ca6e8
-  free (proc_fd_path);
6ca6e8
   if (file_path_length < 0)
6ca6e8
     FAIL_EXIT1 ("readlink (%s, %p, %zu)", proc_fd_path, file_path,
6ca6e8
 		sizeof (file_path));
6ca6e8
+
6ca6e8
+  free (proc_fd_path);
6ca6e8
   file_path[file_path_length] = '\0';
6ca6e8
   TEST_COMPARE_STRING (file_path, "/dev/null");
6ca6e8
 }