Bug 15704 – @safe code should not allow copying to/from void[]

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2016-02-18T23:05:34Z
Last change time
2021-03-18T07:19:57Z
Keywords
accepts-invalid, pull, safe
Assigned to
No Owner
Creator
hsteoh
See also
https://issues.dlang.org/show_bug.cgi?id=12560

Comments

Comment #0 by hsteoh — 2016-02-18T23:05:34Z
----- void main() @safe { Object[] objs = [ new Object() ]; void[] arr1 = objs; void[] arr2 = [ 123, 345, 567 ]; arr1[] = arr2[]; // overwrites pointers with arbitrary ints } ----- It should be illegal to copy the contents of one void[] to another void[], since void[] by definition is a type-erased array and can represent any arbitrary type, including types with indirections. Since type information has been erased, there is no way to verify that the destination array has no indirections, so to guarantee @safety, such an operation must not be allowed in @safe code.
Comment #1 by nick — 2016-06-13T21:22:03Z
Shouldn't we just disallow all writes to a void[] in safe code?
Comment #2 by hsteoh — 2016-06-14T15:15:55Z
It's not just writing to void[] that's the problem. Consider: ---- int[] intArr = [ 1,2,3,4,5 ]; void[] voidArr = intArr; // OK, every array converts to void[] int*[] ptrArr; ptrArr.length = 5; ptrArr[] = voidArr[]; // reinterpret intArr as pointers ptrArr[0] = 1; // oops ---- Basically, *anything* that leads to reinterpretation of something as pointer values cannot be allowed in @safe.
Comment #3 by schveiguy — 2016-06-14T15:26:44Z
(In reply to hsteoh from comment #2) > It's not just writing to void[] that's the problem. Consider: > > ---- > int[] intArr = [ 1,2,3,4,5 ]; > void[] voidArr = intArr; // OK, every array converts to void[] > int*[] ptrArr; > ptrArr.length = 5; > ptrArr[] = voidArr[]; // reinterpret intArr as pointers Wait, does this really work (I didn't think it did)? If so, isn't it still implicitly doing this: (cast(void[])ptrArr)[] = voidArr[]; Which is still writing void data.
Comment #4 by hsteoh — 2016-06-14T15:40:34Z
Oh, you're right, it doesn't compile because implicit conversion from void[] to int*[] is not allowed. OK, nevermind what I said, then. :-D
Comment #5 by bugzilla — 2016-06-20T08:26:40Z
Comment #6 by github-bugzilla — 2016-06-21T11:22:47Z
Commits pushed to master at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/378e6e3ff01e8e1afd5b5bb97d259ae68918ef9e fix Issue 15704 - @safe code should not allow copying to/from void[] https://github.com/dlang/dmd/commit/8ed696695c913234d7bed276215c9dcae8a9cc66 Merge pull request #5877 from WalterBright/fix15704 fix Issue 15704 - @safe code should not allow copying to/from void[]
Comment #7 by github-bugzilla — 2016-10-01T11:47:55Z
Commits pushed to stable at https://github.com/dlang/dmd https://github.com/dlang/dmd/commit/378e6e3ff01e8e1afd5b5bb97d259ae68918ef9e fix Issue 15704 - @safe code should not allow copying to/from void[] https://github.com/dlang/dmd/commit/8ed696695c913234d7bed276215c9dcae8a9cc66 Merge pull request #5877 from WalterBright/fix15704