@safe and null dereferencing

Steven Schveighoffer via Digitalmars-d digitalmars-d at puremagic.com
Thu Jul 27 11:12:11 PDT 2017


On 7/27/17 1:52 PM, H. S. Teoh via Digitalmars-d wrote:
> On Thu, Jul 27, 2017 at 11:03:02AM -0400, Steven Schveighoffer via Digitalmars-d wrote:
> [...]
>> However, there do exist places where dereferencing null may NOT cause
>> a segmentation fault. For example, see this post by Moritz Maxeiner:
>> https://forum.dlang.org/post/udkdqogtrvanhbotdoik@forum.dlang.org
>>
>> In such cases, the compiled program can have no knowledge that the
>> zero page is mapped somehow. There is no way to prevent it, or
>> guarantee it during compilation.
> [...]
> 
> There is one flaw with Moritz's example: if the zero page is mapped
> somehow, that means 0 is potentially a valid address of a variable, and
> therefore checking for null is basically not only useless but wrong: a
> null check of the address of this variable will fail, yet the pointer is
> actually pointing at a valid address that just happens to be 0.  IOW, if
> the zero page is mapped, we're *already* screwed anyway, might as well
> just give up now.

Very true. You wouldn't want to store anything there as any @safe code 
could easily get a pointer to that data at any time!

Either way, the guarantees of @safe go out the window if dereferencing 
null is not a crashing error.

-Steve


More information about the Digitalmars-d mailing list