dmd's optimizer detects intraprocedural cases of null dereferences

Walter Bright newshound2 at digitalmars.com
Sat Jul 27 19:02:19 UTC 2024


BTW,

```
void func()
{
     int* p = null;
     *p = 3;
}
```

```
dmd -c null.d -vasm
_D4null4funcFZv:
0000:   31 C0                    xor       EAX,EAX
0002:   C7 00 03 00 00 00        mov       dword ptr [RAX],3
0008:   C3                       ret
```

```
./cc -c null.d -O
Error: null dereference in function _D4null4funcFZv
```

What is happening here? D's optimizer, as a result of doing a Data Flow Analysis 
called "constant propagation", discovers an attempt to dereference a null pointer.

It does intra-procedural, DFA, not inter-procedural, unless the called functions 
get inlined. (The optimizer runs after inlining.)

----------------

Let's look at gcc:

```
void func()
{
     int* p = (int*)0;
     *p = 3;
}
```

```
 > gcc -c null.c
 > gcc -c null.c -O
 >
```




More information about the Digitalmars-d mailing list