null pointer dereference detection in DMD
Richard (Rikki) Andrew Cattermole
richard at cattermole.co.nz
Sat Jan 11 02:01:50 UTC 2025
On 11/01/2025 2:43 PM, Walter Bright wrote:
> Curiously, compiling it with `gcc -O3` does not detect it. ImportC
> doesn't detect it, either, a choice made because some C code uses such a
> construct as a way to drop into the debugger.
Yeah you didn't turn the warning on.
```c
int main() {
int* ptr;
*ptr = 3;
return 0;
}
```
flags: ``-Wnull-dereference -O``
```
<source>: In function 'main':
<source>:5:10: warning: null pointer dereference [-Wnull-dereference]
5 | *ptr = 3;
| ~~~~~^~~
ASM generation compiler returned: 0
<source>: In function 'main':
<source>:5:10: warning: null pointer dereference [-Wnull-dereference]
5 | *ptr = 3;
| ~~~~~^~~
Execution build compiler returned: 0
Program returned: 139
Program terminated with signal: SIGSEGV
```
https://gcc.gnu.org/onlinedocs/gcc/Warning-Options.html
"Warn 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."
More information about the Digitalmars-d
mailing list