Thread overview
[Issue 20722] typeid(X).initializer() breaks safety
Apr 06, 2020
Rainer Schuetze
Apr 07, 2020
Mathias LANG
Apr 07, 2020
Rainer Schuetze
Apr 08, 2020
Mathias LANG
Apr 08, 2020
Mathias LANG
Dec 17, 2022
Iain Buclaw
April 06, 2020
https://issues.dlang.org/show_bug.cgi?id=20722

Rainer Schuetze <r.sagitario@gmx.de> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |r.sagitario@gmx.de

--- Comment #1 from Rainer Schuetze <r.sagitario@gmx.de> ---
This is as safe/unsafe as any null pointer:

struct S { int a, b; }
void main() @safe
{
    S* s;
    s.b = 3;
}

null pointer accesses and anything in the vicinity are not considered a safety problem because they are supposed to cause an access violation, not a memory corruption.

--
April 07, 2020
https://issues.dlang.org/show_bug.cgi?id=20722

--- Comment #2 from Mathias LANG <pro.mathias.lang@gmail.com> ---
Not quite. It's true the `null` pointers are not considered unsafe, otherwise we wouldn't be able to dereference anything in `@safe` code.

But one of the guarantee that `@safe` code offers is that you cannot create an invalid, non null pointer. That's why, for example, you can't do `arr.ptr`, but you can do `&arr[idx].ptr`.

And if you do:
```
    const void* ptr = &typeid(int).initializer()[$-1];
    assert(ptr !is null);
    writeln(ptr);
```

You create a pointer with value `0x3`, which breaks `@safe`.

--
April 07, 2020
https://issues.dlang.org/show_bug.cgi?id=20722

--- Comment #3 from Rainer Schuetze <r.sagitario@gmx.de> ---
As my example tried to demonstrate, you can do this with a null pointer to
struct (or class) aswell:

import std.stdio;
struct S { int a, b; }
void main() @safe
{
    S* s;
    int* p = &s.b;
    writeln(p); // 4
    *p = 3;     // segfault
}

--
April 08, 2020
https://issues.dlang.org/show_bug.cgi?id=20722

Mathias LANG <pro.mathias.lang@gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           See Also|                            |https://issues.dlang.org/sh
                   |                            |ow_bug.cgi?id=20725

--
April 08, 2020
https://issues.dlang.org/show_bug.cgi?id=20722

--- Comment #4 from Mathias LANG <pro.mathias.lang@gmail.com> ---
I see what you mean now.
Yes indeed, you can do this with null pointer and struct, but it seems like a
bug too.
Filed as https://issues.dlang.org/show_bug.cgi?id=20725

--
December 17, 2022
https://issues.dlang.org/show_bug.cgi?id=20722

Iain Buclaw <ibuclaw@gdcproject.org> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Priority|P1                          |P3

--