bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH 1/2] explicit_bzero-tests: pacify GCC


From: Paul Eggert
Subject: Re: [PATCH 1/2] explicit_bzero-tests: pacify GCC
Date: Sun, 18 Jul 2021 18:17:21 -0500
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.11.0

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 via email to

[Prev in Thread] Current Thread [Next in Thread]