public inbox for glibc-cvs@sourceware.org
help / color / mirror / Atom feed
From: Adhemerval Zanella <azanella@sourceware.org>
To: glibc-cvs@sourceware.org
Subject: [glibc/azanella/clang] malloc: Avoid compiler optimize away malloc calls
Date: Thu, 31 Mar 2022 19:11:25 +0000 (GMT)	[thread overview]
Message-ID: <20220331191125.486EE3886C4A@sourceware.org> (raw)

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.");


                 reply	other threads:[~2022-03-31 19:11 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220331191125.486EE3886C4A@sourceware.org \
    --to=azanella@sourceware.org \
    --cc=glibc-cvs@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).