Bug 20722 – typeid(X).initializer() breaks safety

Status
NEW
Severity
normal
Priority
P3
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2020-04-06T12:24:52Z
Last change time
2024-12-13T19:08:01Z
Keywords
safe
Assigned to
No Owner
Creator
Mathias LANG
See also
https://issues.dlang.org/show_bug.cgi?id=20725, https://issues.dlang.org/show_bug.cgi?id=5176
Moved to GitHub: dmd#17950 →

Comments

Comment #0 by pro.mathias.lang — 2020-04-06T12:24:52Z
``` import std.stdio; void main () @safe { writeln(typeid(int).initializer()); } ``` SEGV because `initiializer` is `@trusted` and returns a slice of length 4 from 0x0 to 0x4.
Comment #1 by r.sagitario — 2020-04-06T18:52:00Z
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.
Comment #2 by pro.mathias.lang — 2020-04-07T03:46:20Z
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`.
Comment #3 by r.sagitario — 2020-04-07T20:38:16Z
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 }
Comment #4 by pro.mathias.lang — 2020-04-08T03:23:45Z
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
Comment #5 by robert.schadek — 2024-12-13T19:08:01Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/17950 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB