public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
* [glibc/azanella/clang] malloc: Avoid compiler optimize away malloc calls
@ 2022-03-31 19:11 Adhemerval Zanella
  0 siblings, 0 replies; only message in thread
From: Adhemerval Zanella @ 2022-03-31 19:11 UTC (permalink / raw)
  To: glibc-cvs

https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=8ceab8c83a254712642274cb5470ed89bf8632ac

commit 8ceab8c83a254712642274cb5470ed89bf8632ac
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
Date:   Wed Mar 30 15:42:18 2022 -0300

    malloc: Avoid compiler optimize away malloc calls
    
    The tests still shows
    
    Error: errno is not set correctly

Diff:
---
 malloc/tst-malloc.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

diff --git a/malloc/tst-malloc.c b/malloc/tst-malloc.c
index 62e3b9b95c..b80c68b448 100644
--- a/malloc/tst-malloc.c
+++ b/malloc/tst-malloc.c
@@ -33,6 +33,12 @@ static int
 do_test (void)
 {
   void *p, *q;
+  /* The unused sink variable is used to force compiler to not
+     optimize away invalid malloc calls.  */
+  DIAG_PUSH_NEEDS_COMMENT;
+  DIAG_IGNORE_NEEDS_COMMENT (7, "-Wunused-but-set-variable");
+  void *volatile sink = 0;
+  DIAG_POP_NEEDS_COMMENT;
   int save;
 
   errno = 0;
@@ -44,8 +50,10 @@ do_test (void)
   DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
 #endif
   p = malloc (-1);
+  sink = p;
   DIAG_POP_NEEDS_COMMENT;
   save = errno;
+  asm volatile("" ::: "memory");
 
   if (p != NULL)
     merror ("malloc (-1) succeeded.");
@@ -59,14 +67,17 @@ do_test (void)
 
   /* realloc (p, 0) == free (p).  */
   p = realloc (p, 0);
+  sink = p;
   if (p != NULL)
     merror ("realloc (p, 0) failed.");
 
   p = malloc (0);
+  sink = p;
   if (p == NULL)
     merror ("malloc (0) failed.");
 
   p = realloc (p, 0);
+  sink = p;
   if (p != NULL)
     merror ("realloc (p, 0) failed.");
 
@@ -81,6 +92,7 @@ do_test (void)
   DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
 #endif
   q = malloc (-512 * 1024);
+  sink = q;
   DIAG_POP_NEEDS_COMMENT;
   if (q != NULL)
     merror ("malloc (-512K) succeeded.");


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-03-31 19:11 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-03-31 19:11 [glibc/azanella/clang] malloc: Avoid compiler optimize away malloc calls Adhemerval Zanella

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).