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

Reply via email to