Why does nobody seem to think that `null` is a serious problem in D?

Steven Schveighoffer schveiguy at gmail.com
Mon Nov 19 21:52:47 UTC 2018


On 11/19/18 4:23 PM, Jordi Gutiérrez Hermoso wrote:
> When I was first playing with D, I managed to create a segfault by doing 
> `SomeClass c;` and then trying do something with the object I thought I 
> had default-created, by analogy with C++ syntax. Seasoned D programmers 
> will recognise that I did nothing of the sort and instead created c is 
> null and my program ended up dereferencing a null pointer.
> 
> I'm not the only one who has done this. I can't find it right now, but 
> I've seen at least one person open a bug report because they 
> misunderstood this as a bug in dmd.
> 
> I have been told a couple of times that this isn't something that needs 
> to be patched in the language, but I don't understand. It seems like a 
> very easy way to generate a segfault (and not a NullPointerException or 
> whatever).
> 
> What's the reasoning for allowing this?

A null pointer dereference is an immediate error, and it's also a safe 
error. It does not cause corruption, and it is free (the MMU is doing it 
for you).

Note, you can get a null pointer exception on Linux by using 
etc.linux.memoryerror: 
https://github.com/dlang/druntime/blob/master/src/etc/linux/memoryerror.d

The worst part about a null-pointer segfault is when it's intermittent 
and you get no information about where it happens. Then it can be 
annoying to track down. But it can't be used as an exploit.

Consistent segfaults are generally easy to figure out.

-Steve


More information about the Digitalmars-d-learn mailing list