https://gcc.gnu.org/bugzilla/show_bug.cgi?id=87392

--- Comment #6 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Eugeniu Rosca from comment #5)
> > In C90, it was implemented defined behavior (while in C99 and above it is 
> > undefined behavior).
> 
> I trust you, but why not giving a reference to the source?

He already did. Comment 1 quotes the GCC manual which references the relevant
sections of the standards.

It takes time and effort to look the references up in the old standards. If you
need more precise answers *you* should look it up.

In C90 it's 6.3.7 (and repeated in G.3.5), in C99 it's 6.5.7 (and J.2).


> - Since U-Boot is compiled using '-std=gnu11' and since UBSAN reports (1 <<
> 31) as undefined behavior and since C11 standard says that encountering an
> UB the compiler may "ignore the situation completely with unpredictable
> results",

It may do that, or it may do other things (like print a UBsan error).

> what are there real-life issues expected from shifting signed
> integers into (*not past*) the sign bit? What to expect in the worst case?

I think with GCC the worst that will happen is you get a UBsan error (which
might make the program abort, depending on the options used to compile it).

> - Is it possible to provide a sample code which would showcase that
> "left-shifting into the sign bit" can lead to program behavior being
> erroneous, unexpected or dependent on the gcc optimization level? For
> example, we know [17] that "(a + 1 > a)" can lead to surprising results
> depending on whether the program is compiled with "-fwrapv" or not. Are
> there any similar examples involving '<<' ? This would be of extreme help
> for U-Boot community.

You've already been given the link to the GCC documentation which says that GCC
doesn't treat it as undefined in C99 and C11, even though the standards (and
UBsan) say it's undefined.

There is no bug here. It's correct for UBsan to depend on the C standard
chosen, because what is and isn't undefined depends on the standard.

> - What would be your personal choice between '-std=gnu89' (still used by
> Linux kernel) and '-std=gnu11' (used in U-Boot/coreboot), given your GCC
> experience gathered so far?

There's no one-size-fits-all answer.

Reply via email to