Bug 20941 – __traits(getMember) should not allow safe code to access private fields

Status
NEW
Severity
enhancement
Priority
P4
Component
dmd
Product
D
Version
D2
Platform
x86
OS
Mac OS X
Creation time
2020-06-17T12:38:03Z
Last change time
2024-12-13T19:09:16Z
Assigned to
No Owner
Creator
Andrei Alexandrescu
Moved to GitHub: dmd#19721 →

Comments

Comment #0 by andrei — 2020-06-17T12:38:03Z
This is taken from DIP 1035: struct S { private union { int x; int* y; } private int tag; // x if 0, y if 1 // public @trusted interface follows void set(int rhs) @trusted { if (tag == 0) x = rhs; else *y = rhs; } .... } Allowing safe code to modify tag from @safe code, followed by calling set(), results in unsafe code from safe functions. This could be relaxed to restrict access only for types that have @trusted functions.
Comment #1 by stanislav.blinov — 2020-06-17T20:18:57Z
For clarity, you mean modifying `tag` like so? void unsafe() @safe { S s; __traits(getMember, s, "tag") = 2; // should be error s.set(42); }
Comment #2 by robert.schadek — 2024-12-13T19:09:16Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/19721 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB