bug-gnulib@gnu.org mirror (unofficial)
 help / color / mirror / Atom feed
From: Paul Eggert <eggert@cs.ucla.edu>
To: Bruno Haible <bruno@clisp.org>
Cc: bug-gnulib@gnu.org
Subject: Re: [PATCH 1/2] explicit_bzero-tests: pacify GCC
Date: Sun, 18 Jul 2021 18:17:21 -0500	[thread overview]
Message-ID: <7d3aed6a-e6f5-e5cb-373a-d5dec068ea0b@cs.ucla.edu> (raw)
In-Reply-To: <2568272.mkVSEl9qYJ@omega>

On 7/18/21 5:23 PM, Bruno Haible wrote:
>
> Such compiler optimizations would need to be backed by the standards.
> Are there initiatives to "outlaw" references to uninitialized storage
> in recent C or C++ standards?

No initiatives are needed, at least for C. Using uninitialized storage 
is undefined behavior in the current C standard and this has been true 
ever since C was standardized. I imagine C++ is similar.


>
> I hope code such as
>
>    int x; /* uninitialized */
>    if (!((x & 1) == 0 || (x & 1) == 1))
>      abort ();
>
> will never crash. x & 1 can only be 0 or 1. Tertium not datur.

The C standard doesn't guarantee that code will never crash. For 
example, the standard allows an implementation that uses two's 
complement but where INT_MIN == -INT_MAX and where the bit pattern 
0x80000000 is a trap value (i.e., your program aborts if it reads an int 
whose machine value is 0x80000000).


> GCC does not _know_ that the array
> is uninitialized. It's only a "maybe uninitialized".

That's what GCC's diagnostic says, yes. But in cases like these GCC 
actually "knows" that variables are uninitialized and it sometimes 
optimizes based on this knowledge. For example, for:

   _Bool f (void) {  char *p; return !p; }

gcc -O2 (GCC 11.1.1 20210531 (Red Hat 11.1.1-3)) "knows" that P is 
uninitialized and generates code equivalent to that of:

   _Bool f (void) { return 1; }

That is, GCC optimizes away the access to p's value, which GCC can do 
because the behavior is undefined.


>   If GCC ever
> infers that it is "certainly uninitialized", we could defeat that
> through a use of 'volatile', such as

Yes, some use of volatile should do the trick for GCC (which is what my 
patch did). However, one would still have problems with a debugging 
implementation, e.g., if GCC ever supports an -fsanitize=uninitialized 
option that catches use of uninitialized storage.

This is all low priority of course.



  reply	other threads:[~2021-07-18 23:17 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-07-18  4:56 [PATCH 1/2] explicit_bzero-tests: pacify GCC Paul Eggert
2021-07-18  4:56 ` [PATCH 2/2] memrchr-tests: " Paul Eggert
2021-07-18  9:12 ` [PATCH 1/2] explicit_bzero-tests: " Bruno Haible
2021-07-18 19:14   ` Paul Eggert
2021-07-18 22:23     ` Bruno Haible
2021-07-18 23:17       ` Paul Eggert [this message]
2021-07-19  0:37         ` Bruno Haible

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

  List information: https://lists.gnu.org/mailman/listinfo/bug-gnulib

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

  git send-email \
    --in-reply-to=7d3aed6a-e6f5-e5cb-373a-d5dec068ea0b@cs.ucla.edu \
    --to=eggert@cs.ucla.edu \
    --cc=bruno@clisp.org \
    --cc=bug-gnulib@gnu.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).