dmd's optimizer detects intraprocedural cases of null dereferences

kdevel kdevel at vogtner.de
Sat Jul 27 19:53:39 UTC 2024


On Saturday, 27 July 2024 at 19:02:19 UTC, Walter Bright wrote:
> [...]
> Let's look at gcc:
>
> ```
> void func()
> {
>     int* p = (int*)0;
>     *p = 3;
> }
> ```
>
> ```
> > gcc -c null.c
> > gcc -c null.c -O
> >
> ```

$ gcc -c -O -Wnull-dereference null.c
null.c: In function 'func':
null.c:4:8: warning: null pointer dereference [-Wnull-dereference]
     4 |     *p = 3;
       |     ~~~^~~

It seems that at least optimization O1 is required to trigger the 
detection and -Wnull-dereference seems not to be included in 
-Wall -pedantic. Works since GCC 6.5.0.

| -Wnull-dereference warns if the compiler detects paths that
| trigger erroneous or undefined behavior due to dereferencing a 
null
| pointer. This option is only active when 
-fdelete-null-pointer-checks
| is active, which is enabled by optimizations in most targets. 
The
| precision of the warnings depends on the optimization options 
used.

[1] https://gcc.gnu.org/gcc-6/changes.html


More information about the Digitalmars-d mailing list