public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/67602] New: unsigned left-shift overflow should not be undefined in c++
@ 2015-09-16 18:15 huntting at glarp dot com
  2015-09-16 18:52 ` [Bug c++/67602] " trippels at gcc dot gnu.org
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: huntting at glarp dot com @ 2015-09-16 18:15 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 67602
           Summary: unsigned left-shift overflow should not be undefined
                    in c++
           Product: gcc
           Version: 4.8.5
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: huntting at glarp dot com
  Target Milestone: ---

Created attachment 36343
  --> https://gcc.gnu.org/bugzilla/attachment.cgi?id=36343&action=edit
example.cc

For an unsigned E1, E1<<E2 should be equal to E1*2^E2 mod 2^N (where N is the
size of E1). There is no restriction on E2 except that it must be non-negative.
Hence ~0U<<32 should always be 0 if ints are 32bits.

Unfortunately, when I compile the attached example.cc on amd64 with -O0, I get
this:

                  sizeof(0ULL) = 8
                         ~0ULL = 0xffffffffffffffff
     ~0ULL << (8*sizeof(0ULL)) = 0xffffffffffffffff
  ~(~0ULL << (8*sizeof(0ULL))) = 0
                    sizeof(0U) = 4
                           ~0U = 0xffffffff
         ~0U << (8*sizeof(0U)) = 0xffffffff
      ~(~0U << (8*sizeof(0U))) = 0

Curiously, enabling -O1 fixes the problem. I have also seen this behavior when
the expression is not known at compile time but I've not tested under various
-O levels in that case.

It should be noted that the C standard appears to allow for undefined behavior
when E2>=N.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/67602] unsigned left-shift overflow should not be undefined in c++
  2015-09-16 18:15 [Bug c++/67602] New: unsigned left-shift overflow should not be undefined in c++ huntting at glarp dot com
@ 2015-09-16 18:52 ` trippels at gcc dot gnu.org
  2015-09-16 18:56 ` miyuki at gcc dot gnu.org
  2015-09-16 19:30 ` huntting at glarp dot com
  2 siblings, 0 replies; 4+ messages in thread
From: trippels at gcc dot gnu.org @ 2015-09-16 18:52 UTC (permalink / raw)
  To: gcc-bugs

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

Markus Trippelsdorf <trippels at gcc dot gnu.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|UNCONFIRMED                 |RESOLVED
                 CC|                            |trippels at gcc dot gnu.org
         Resolution|---                         |INVALID

--- Comment #1 from Markus Trippelsdorf <trippels at gcc dot gnu.org> ---
It should be noted that the C standard appears to allow for undefined behavior
when E2>=N.

It is undefined behavior in C++, too.


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/67602] unsigned left-shift overflow should not be undefined in c++
  2015-09-16 18:15 [Bug c++/67602] New: unsigned left-shift overflow should not be undefined in c++ huntting at glarp dot com
  2015-09-16 18:52 ` [Bug c++/67602] " trippels at gcc dot gnu.org
@ 2015-09-16 18:56 ` miyuki at gcc dot gnu.org
  2015-09-16 19:30 ` huntting at glarp dot com
  2 siblings, 0 replies; 4+ messages in thread
From: miyuki at gcc dot gnu.org @ 2015-09-16 18:56 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #2 from Mikhail Maltsev <miyuki at gcc dot gnu.org> ---
(In reply to Brad Huntting from comment #0)
> It should be noted that the C standard appears to allow for undefined
> behavior when E2>=N.

And so does the C++ standard:

"... The behavior is undefined if the right operand is negative, or greater
than or equal to the length in bits of the promoted left operand." [expr.shift]
(5.8.1).


^ permalink raw reply	[flat|nested] 4+ messages in thread

* [Bug c++/67602] unsigned left-shift overflow should not be undefined in c++
  2015-09-16 18:15 [Bug c++/67602] New: unsigned left-shift overflow should not be undefined in c++ huntting at glarp dot com
  2015-09-16 18:52 ` [Bug c++/67602] " trippels at gcc dot gnu.org
  2015-09-16 18:56 ` miyuki at gcc dot gnu.org
@ 2015-09-16 19:30 ` huntting at glarp dot com
  2 siblings, 0 replies; 4+ messages in thread
From: huntting at glarp dot com @ 2015-09-16 19:30 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from Brad Huntting <huntting at glarp dot com> ---
Oops. I missed that part. Thanks.

brad

On Wed, Sep 16, 2015 at 12:56 PM, miyuki at gcc dot gnu.org <
gcc-bugzilla@gcc.gnu.org> wrote:

> https://gcc.gnu.org/bugzilla/show_bug.cgi?id=67602
>
> --- Comment #2 from Mikhail Maltsev <miyuki at gcc dot gnu.org> ---
> (In reply to Brad Huntting from comment #0)
> > It should be noted that the C standard appears to allow for undefined
> > behavior when E2>=N.
>
> And so does the C++ standard:
>
> "... The behavior is undefined if the right operand is negative, or greater
> than or equal to the length in bits of the promoted left operand."
> [expr.shift]
> (5.8.1).
>
> --
> You are receiving this mail because:
> You reported the bug.
>


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2015-09-16 19:30 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-09-16 18:15 [Bug c++/67602] New: unsigned left-shift overflow should not be undefined in c++ huntting at glarp dot com
2015-09-16 18:52 ` [Bug c++/67602] " trippels at gcc dot gnu.org
2015-09-16 18:56 ` miyuki at gcc dot gnu.org
2015-09-16 19:30 ` huntting at glarp dot com

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