Bug 17507 – Associative Array range operations should be marked @safe

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-06-15T13:15:21Z
Last change time
2017-11-06T14:31:44Z
Keywords
safe
Assigned to
Lucia Cojocaru
Creator
Steven Schveighoffer
See also
https://issues.dlang.org/show_bug.cgi?id=17108

Comments

Comment #0 by schveiguy — 2017-06-15T13:15:21Z
All the operations *can* be safe, but are not currently. All problems happen when the AA is rehashed. For instance, if the AA is rehashed, the range may be referring to an empty bucket. In this case, the element is null. Fetching the key returns a null pointer (safe), fetching the value, returns a null pointer + sizeof(key) (unsafe). We can fix this, and then mark the front call safe completely. Note that the current front call is @trusted, that should be removed and the actual call into the runtime marked @safe. Another issue: if the rehash shrinks the array, then the index could be out of bounds. Since druntime is compiled in release mode without bounds checks, the range could corrupt memory if used at that point. It would cost very little to check the index against the length of the array before returning.
Comment #1 by schveiguy — 2017-11-06T14:31:44Z
This is effectively fixed by https://github.com/dlang/druntime/pull/1944