llvm / llvm/llvm-project

`-fwrapv` incorrectly disables `-fsanitize=undefined` catching `-2147483648 % -1`

Open
#200,152 4 comments 0 reactions 0 assignees View on GitHub
compiler-rt:ubsan llvm:optimizations
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

This program:

```c
int main() {
int a = 1U << 31;
int b = a % -1;
}
```

compiled with `-fwrapv -fsanitize=undefined`, crashes with `UndefinedBehaviorSanitizer: FPE on unknown address`, instead of properly detecting and reporting overflow as it does when ran without `-fwrapv` (note that `-fwrapv` does not prevent this operation being UB - the program still `SIGFPE`s when compiled with just `-fwrapv`).

And, combined with `-O1` or greater, will result in no sanitizer warnings at all.

The same applies to division:

```c
int main() {
int a = 1U << 31;
int b = a / -1;
}
```

https://godbolt.org/z/h9GK47fK7

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.