null pointer dereference detection in DMD

Derek Fawcus dfawcus+dlang at employees.org
Sat Jan 11 15:29:46 UTC 2025


Interesting, I get a different error from GCC:


```
$ gcc -g -O2 -std=c11 -Wall -Wextra -Wpedantic -Werror null.c
null.c:1:6: error: return type of ‘main’ is not ‘int’ 
[-Werror=main]
     1 | void main()
       |      ^~~~
null.c: In function ‘main’:
null.c:4:8: error: ‘p’ is used uninitialized 
[-Werror=uninitialized]
     4 |     *p = 3;
       |     ~~~^~~
cc1: all warnings being treated as errors
```

Which is more accurate, as there is no guarantee an uninitialised 
auto pointer will actually be NULL, rather than arbitrary garbage.

That is GCC 11.4.0, using GCC 12.3.0 adds:

```
null.c:3:10: note: ‘p’ was declared here
     3 |     int* p;
       |          ^
cc1: all warnings being treated as errors
```

After correcting the return type, and adding a return 0, clang 
14.0.0 gives me:

```
$ clang-14 -g -O2 -std=c11 -Wall -Wextra -Wpedantic -Werror null.c
null.c:4:6: error: variable 'p' is uninitialized when used here 
[-Werror,-Wuninitialized]
     *p = 3;
      ^
null.c:3:11: note: initialize the variable 'p' to silence this 
warning
     int* p;
           ^
            = 0
1 error generated.
```



More information about the Digitalmars-d mailing list