<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/61522>61522</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
UBsan produces incorrect report on a null-pointer-dereference
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</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 cannot produce an incorrect report on the null-pointer-dereference. GCC'UBsan can produce the correct report.
Compiler explorer: https://godbolt.org/z/949vYEaMb
```shell
% cat a.c
struct a {
char d
} ;
struct a b;
struct a *c;
struct a e() {
int f[3];
f[2]=0;
c = 0;
f[2]=0;
*c = b;
return b;
}
void main() {
e();
}
%
% clang -fsanitize=undefined -O0 a.c && ./a.out
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address
...
% clang -fsanitize=undefined -O2 a.c && ./a.out
/a.c:8:3: runtime error: subtraction of unsigned offset from 0x7ffcd03c3e2c overflowed to 0x7ffcd03c3e34
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /a.c:8:3 in
/a.c:10:3: runtime error: subtraction of unsigned offset from 0x7ffcd03c3e2c overflowed to 0x7ffcd03c3e34
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /a.c:10:3 in
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer
...
%
% gcc -fsanitize=undefined -O2 a.c && ./a.out
/a.c:11:6: runtime error: store to null pointer of type 'struct a'
%
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzUVU1v4zgM_TX0hYhhS3EcH3xw6qZYYLoDtOgCPcoyHWtXkQJJbmfm1y_80TZNZ2cWWOxhgCBOnshHPoomhffqYIhKyHaQ1ZEYQm9d6XthV_0gVlpFjW2_lnvrMPSEndXaPitzQGlb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