This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: shift+test optimization error


On 19 January 2017 at 08:03, leon zadorin wrote:
> Last time I checked in C, as well as in C++, standards -- shifting by
> the number of bits greater *or equal* to the size of the type is
> undefined behavior...
>
> e.g. something like "An expression is shifted by a negative number or
> by an amount greater than or equal to the width of the promoted
> expression (6.5.7)." from
> https://www.securecoding.cert.org/confluence/display/c/CC.+Undefined+Behavior#CC.UndefinedBehavior-ub_51
>
> The exact place in C++14 and or latest C standards escape me right
> now, but the thesis of this UB stands correct I think...

Yes, and it's easily confirmed by compiling with -fsanitize=undefined,
so that it flags the error at runtime:

60=>0x2000000000000000
61=>0x4000000000000000
ub.c:11:25: runtime error: left shift of 1 by 63 places cannot be
represented in type 'long int'
62=>0x8000000000000000
63=>0x0
0=>0x2

So it's not an optimization bug, the testcase has undefined behaviour.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]