<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/60236>60236</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            False negative: ubsan failed to detect 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 at -O0 did not produce any useful information while -O1 and above produced the wrong information.

GCC correctly reported the Null-pointer-dereference at all optimization levels.

Compiler explorer: https://godbolt.org/z/K69dG83PE

```console
% cat a.c
int main() {
    int *a = 0;
    int b[3]={1, 1, 1};
    ++b[2];
 ++*a;
}
% clang -fsanitize=undefined -O0 a.c &&./a.out
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000 (pc 0x55ae84c2b8a8 bp 0x7ffdb62e7d70 sp 0x7ffdb62e7d20 T1)
==1==The signal is caused by a READ memory access.
==1==Hint: address points to the zero page.
    #0 0x55ae84c2b8a8  (/app/output.s+0x2b8a8)
    #1 0x7f4c2178a082  (/lib/x86_64-linux-gnu/libc.so.6+0x24082) (BuildId: 1878e6b475720c7c51969e69ab2d276fae6d1dee)
    #2 0x55ae84c0436d (/app/output.s+0x436d)

UndefinedBehaviorSanitizer can not provide additional info.
SUMMARY: UndefinedBehaviorSanitizer: SEGV (/app/output.s+0x2b8a8) 
==1==ABORTING
% clang -fsanitize=undefined -O1 a.c &&./a.out
/a.c:4:5: runtime error: signed integer overflow: 1 + 1 cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /app/example.c:4:5 in 
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==1==ERROR: UndefinedBehaviorSanitizer: stack-overflow on address 0x7ffc9b3dffe0 (pc 0x7ffc9b3dffe0 bp 0x000000000000 sp 0x7ffc9b3dfee8 T1)
    #0 0x7ffc9b3dffe0  ([stack]+0x1efe0)

SUMMARY: UndefinedBehaviorSanitizer: stack-overflow ([stack]+0x1efe0) 
==1==ABORTING
%
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8Vl9v4roT_TTmZZTIcUISHniAUvpb_Xa3V-3ulfbpyrEn4LvGjmyH0n76KydQKNo_vS-3qkDMeMZnZo6Pzb1XG4M4J9Mlma4mvA9b6-Z-y22y7Xmi1aSx8nm-tg7CFqG1WtsnZTYgrETCbuBGc7MhrPLwdem5AR4guacglQRjA3TOyl4gcPMMvce216BMa92OB2UNPG2VRkjuM-BGAm_sHk8hctjwyVmzuQxJCV0Ruhg_725uQFjnUAT9DA4768Ix8HOvddJZZQK6RKLDFh2aiCQA1xpsF9ROvYwwNO5R-zepb-yuUxod4KHT1qEj-QK2IXSe5AvC1oStN1Y2VofUug1h6xfC1v8vZ_Kuzv-4vcxESjr-C2u81Xi0simIiCUVo0GZADuuDGE1YTMg1XK0AwBEH2ELDiRfASX5lash02VOpiuSr0i1zOJYjh_V6s1iwpaELeNyNiw_uUZ73OHVFkPPQOOQIWk9NyqoFyT5qjcSW2VQDuPmqQDCSsLKlLA1T20fxuivp2VL3PK9su7xmCL2c3W7WH389vjh7vPi43GzPBaRjV-3Dw_3D7Htv0wCj7d3f4I10Jvvxj4Z4FI69B7ogV78AWF1J4AeplOOdSFYU_Mamg7ooWpb2ZQMK1lR8G8tjMKXjLDZD-B92SLE08M1KA-C9x4lNM_A4eF2sYId7qx7Bi4E-ldqXcb_T5kQ8Z_wDmT1EOzA3xd0Fjq-wfRyfDm9rgAGvqx51xG2tn3o-pB6wpb0MPhfoR_js6G6QrCsqjmt2Sleq4aw9aEu_yqLRCvTH5KN6UeHSL1NyzFnQWs20JPVy15p-UHGErK6qrFsimpaMSoqMc1m5QzLGW-YZFXZcixlJhGv0bBzNbTIS_nzYqL3PIbfUA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