public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/115160] New: Enabling undefined behaviour sanitizer causes or'ed bit shift to report wrong result
@ 2024-05-20 10:03 raanan.lori at getnexar dot com
  2024-05-20 17:56 ` [Bug c++/115160] " jakub at gcc dot gnu.org
                   ` (5 more replies)
  0 siblings, 6 replies; 7+ messages in thread
From: raanan.lori at getnexar dot com @ 2024-05-20 10:03 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 115160
           Summary: Enabling undefined behaviour sanitizer causes or'ed
                    bit shift to report wrong result
           Product: gcc
           Version: 9.4.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c++
          Assignee: unassigned at gcc dot gnu.org
          Reporter: raanan.lori at getnexar dot com
  Target Milestone: ---

I've tried to enable gcc undefined behaviour sanitizer recently and one of my
tests encountered an issue where bit shifts in certain code snippet will report
a wrong result, i narrowed it down to this snippet.

I used compiler explorer to find that the same behaviour is also in the latest
release of gcc 14.4, tested with gcc 9.4 and 11.4

This was run under Unbuntu LTS 22.04 x86_64

My compile flags: `g++ -std=c++17 -O0  -fsanitize=undefined -Wall
-fno-sanitize-recover main.cpp"

The main.cpp code to replicate this issue:
```
#include <iostream>
#include <cstdint>
#include <vector>

int main() {
    std::vector<uint8_t> data;    
    data.emplace_back(1);
    data.emplace_back(0);
    data.emplace_back(0);
    data.emplace_back(0);
    auto b = data.begin();

    std::cout << (int)( (*b++)  | (*b++ << 8) | (*b++ << 16 ) | ( *b << 24 )
)<< std::endl;
    return 0;
}
```
expected output should be : 1
actual output: 256
note: no error or warning is produced for undefined behaviour

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

end of thread, other threads:[~2024-05-21 12:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-20 10:03 [Bug c++/115160] New: Enabling undefined behaviour sanitizer causes or'ed bit shift to report wrong result raanan.lori at getnexar dot com
2024-05-20 17:56 ` [Bug c++/115160] " jakub at gcc dot gnu.org
2024-05-21  9:41 ` raanan.lori at getnexar dot com
2024-05-21  9:53 ` jakub at gcc dot gnu.org
2024-05-21 10:07 ` raanan.lori at getnexar dot com
2024-05-21 11:46 ` redi at gcc dot gnu.org
2024-05-21 12:09 ` jakub at gcc dot gnu.org

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