[OT] OT: Null checks.

Walter Bright newshound2 at digitalmars.com
Wed May 7 22:02:29 UTC 2025


Let's break it down:

```
void main() { assert(0); }
```
Compile with -vasm:
```
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   BE 01 00 00 00           mov       ESI,1
0009:   BF 00 00 00 00           mov       EDI,0
000e:   E8 00 00 00 00           call      L0
0013:   31 C0                    xor       EAX,EAX
0015:   5D                       pop       RBP
0016:   C3                       ret
```
The `call L0` is the call to the assert failure function.

Compile with -vasm -release:
```
0000:   0F 0B                    ud2
0002:   C3                       ret
```
The `ud2` is the illegal instruction.

Compile with -vasm -release -checkaction=D:
```
0000:   0F 0B                    ud2
0002:   C3                       ret
```
So what's going on here? First, `-release` turns off all the checking. The 
`assert(0)` means this code should never be reached. But something has to be 
inserted there, so the compiler emits the `ud2`.

It seems to me that if the `assert(0)` has a message, say `assert(0,"kilroy was 
here")`, the checkaction should govern what happens. I'll make that change.

----

I said that -release turns off the checks. Let's turn them on:

Compile with `-vasm -release -check=on`:
```
0000:   55                       push      RBP
0001:   48 8B EC                 mov       RBP,RSP
0004:   BE 01 00 00 00           mov       ESI,1
0009:   BF 00 00 00 00           mov       EDI,0
000e:   E8 00 00 00 00           call      L0
0013:   31 C0                    xor       EAX,EAX
0015:   5D                       pop       RBP
0016:   C3                       ret
```
Next, there's druntime being compiled with `-release`. What does that mean for 
the array bounds check:
```
void main(){
      int[2] x=[0,1];
      auto y=x[1..2];
      x[]=y[];
}
```
Compile with -vasm -release -check=on
```
core.exception.RangeError at x.d(4): Range violation
----------------
??:? [0x40cc4c]
??:? [0x41da17]
??:? [0x40cba5]
??:? [0x41504d]
??:? [0x414fe5]
??:? [0x4070b1]
??:? [0x404f02]
??:? [0x403511]
??:? [0x403487]
??:? [0x4039ce]
??:? [0x40387d]
??:? [0x403956]
??:? [0x40387d]
??:? [0x4037e6]
??:? [0x4035cf]
??:? [0x4034b1]
??:? __libc_start_main [0x7f9b1b129f44]
??:? [0x403368]
```
Compile with -g:
```
core.exception.RangeError at x.d(4): Range violation
----------------
??:? [0x40cc64]
??:? [0x41da2f]
??:? [0x40cbbd]
??:? [0x415065]
??:? [0x414ffd]
??:? [0x4070c9]
??:? [0x404f1a]
??:? [0x403529]
./x.d:4 [0x40348b]
??:? [0x4039e6]
??:? [0x403895]
??:? [0x40396e]
??:? [0x403895]
??:? [0x4037fe]
??:? [0x4035e7]
/home/walter/forks/dmd/druntime/src/core/internal/entrypoint.d:29 [0x4034c7]
??:? __libc_start_main [0x7f310c464f44]
??:? [0x403368]
```
and you get the location of the error.


More information about the Digitalmars-d mailing list