<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/60237>60237</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
False negative: UBsan missed an integer overflow
</td>
</tr>
<tr>
<th>Labels</th>
<td>
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
shao-hua-li
</td>
</tr>
</table>
<pre>
For the following code, Clang's UBsan `-fsanitize=undefined -fno-sanitize-recover=all -O1` missed the integer-overflow, while other opt levels (including -O0) detected it. I checked the produced assembly code and found that the reason was probably that Clang -O1 produced an incorrect guard condition for the multiply operator (43998 instead of 43999 in other opt levels.)
Compiler explorer: https://godbolt.org/z/hr63bjajT
```console
% cat a.c
long a;
short b = 65528;
long *c;
unsigned d() {
for (; 0; 0)
;
for (;;) {
unsigned short e = b;
c = 0;
(e ^= 65535) * 99713;
if (1 ^ a)
break;
b = 0;
for (; 0;)
return 9;
}
++*c;
return 1;
}
int main() { d(); }
%
% clang -fsanitize=address -fno-sanitize-recover=all -O1 a.c && ./a.out
%
% clang -fsanitize=address -fno-sanitize-recover=all -O2 a.c && ./a.out
/a.c:10:18: runtime error: signed integer overflow: 65535 * 99713 cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /a.c:10:18 in
%
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJysVc1u4zYQfhr6MpAhDW3JOujgrCugh8UC3ebQIyWOJWZpUiCppNmnL0jLiuMW7aWGQNvDb2a--ZXwXg2GqGH7J7Y_bcQcRusaPwqbjbPItNp0Vr43rXUQRoKz1dq-KTNAbyUx_AJftDADw8rD85MXBliZZ2cvjArqJzF-mo2kszIkITsbm91uMke9fSXH-EloDdm3gpU5XJT3JJMnZQIN5LIIOmv7Fn29jUoT2DCSAzsF0PRK2gPDgzK9nmXklX3LGdYgKVAfSIIKW_gV-pH6H4vlyVk59yRBeE-XTr-nWEAY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