Issue 157134
Summary [Clang] -Wnull-dereference warning persists with -fno-delete-null-pointer-checks
Labels clang
Assignees
Reporter RichardButurla
    Hi There,

I came across a potential warning bug when building bootloader code with both GCC and Clang using the  ` -fno-delete-null-pointer-checks` flag.

Obviously, Clang is not supposed to be 100% compatible with GCC, however, I wonder if this use-case slipped through the radar.

Clang’s warning  `-Wnull-dereference ` persists even when` -fno-delete-null-pointer-checks `is used, making the flag possibly ineffective from a warning perspective.

**Clang version:** clang 20.1.8 (Fedora 20.1.8-3.fc42)

**Test case:**
```
int main() {
    // Intentional write to address 0 in bootloader context
    *(unsigned long long *)0 = 0xABCDEFAB;
    return 1;
}
```

**Observed Clang Behaviour:**
`
clang -S -O2 -fno-delete-null-pointer-checks null_dereference_test.c`
```

null_dereference_test.c:10:3: warning: indirection of non-volatile null pointer will be deleted, not trap [-Wnull-dereference]
   10 |   *(unsigned long long *)0 = 0xABCDEFAB;
      |   ^~~~~~~~~~~~~~~~~~~~~~~~
null_dereference_test.c:10:3: note: consider using __builtin_trap() or qualifying pointer with 'volatile'
1 warning generated.
```

Clang warns regardless of optimization level or -f(no-)delete-null-pointer-checks.

**Observed GCC behaviour:**
With ` -fno-delete-null-pointer-checks` , GCC does not warn at any optimization level:

`gcc -S -O2 -fno-delete-null-pointer-checks null_dereference_test.c`

GCC does not warn unless `-Wnull-dereference `is explicitly passed and `-fdelete-null-pointer-checks` is active, which is also enabled by optimization levels:

`gcc -S -Oz -fdelete-null-pointer-checks -Wnull-dereference null_dereference_test.c -o opt_delete_null_ptr.s`

```
null_dereference_test.c: In function ‘main’:
null_dereference_test.c:10:28: warning: null pointer dereference [-Wnull-dereference]
   10 |   *(unsigned long long *)0 = 0xABCDEFAB;
      |   ~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~

```
**GCC documentation:**
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#index-fdelete-null-pointer-checks
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html#index-Wnull-dereference

**Why this matters:**

In a bootloader context, writing to address 0 can be intentional and valid.

**Question / Expected Behaviour:**

Should Clang suppress ` -Wnull-dereference ` when `-fno-delete-null-pointer-checks `is specified like GCC?
_______________________________________________
llvm-bugs mailing list
llvm-bugs@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to