llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
29.11k stars 12.01k forks source link

UBSan is inconsistent about erroring on dead code in ternary operator branches #51612

Open regehr opened 3 years ago

regehr commented 3 years ago
Bugzilla Link 52270
Version trunk
OS Linux
CC @pascal-cuoq,@dwblaikie,@RKSimon

Extended Description

https://gcc.godbolt.org/z/WxKjWMo47

in this example program (supplied by Pascal Cuoq), UBSan should either error in both cases, or in neither of them (I would argue that it should not error in either case, but ymmv):

void f(int a) { a ? 0 : 0x7fffffff + 1; }

void g(int a) { a ? 0 : 0 >> 32; }

int main(void) { f(1); g(1); }

instead, however, UBSan errors on the shift bug, but not on the overflowing addition. when this program is run, this is the output we see:

/app/example.c:6:13: runtime error: shift exponent 32 is too large for 32-bit type 'int' SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.c:6:13 in

dwblaikie commented 3 years ago

Yeah, seems like a case where the code is unreachable, so the program does not invoke UB so far as I know - so it's a false positive for UBSan to diagnose it.