all OS functions should be "nothrow @trusted @nogc"
ag0aep6g via Digitalmars-d
digitalmars-d at puremagic.com
Thu Jul 27 06:45:21 PDT 2017
On 07/27/2017 03:24 PM, Moritz Maxeiner wrote:
> --- null.d ---
> version (linux):
>
> import core.stdc.stdio : FILE;
> import core.sys.linux.sys.mman;
>
> extern (C) @safe int fgetc(FILE* stream);
>
> void mmapNull()
> {
> void* mmapNull = mmap(null, 4096, PROT_READ | PROT_WRITE,
> MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED | MAP_POPULATE, -1, 0);
> assert (mmapNull == null, "Do `echo 0 > /proc/sys/vm/mmap_min_addr`
> as root");
> *(cast (char*) null) = 'D';
> }
>
> void nullDeref() @safe
> {
> fgetc(null);
> }
>
> void main(string[] args)
> {
> mmapNull();
> nullDeref();
> }
> ---
>
> For some fun on Linux, try out
> # echo 0 > /proc/sys/vm/mmap_min_addr
> $ rdmd null.d
The gist of this is that Linux can be configured so that null can be a
valid pointer. Right?
That seems pretty bad for @safe at large, not only when C functions are
involved.
More information about the Digitalmars-d
mailing list