[Issue 11176] array.ptr in @safe code
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Wed Feb 24 05:23:24 PST 2016
https://issues.dlang.org/show_bug.cgi?id=11176
--- Comment #9 from Kenji Hara <k.hara.pg at gmail.com> ---
One another way I can think is, array.ptr property would add a hidden check
`arr.length != 0` under @safe code, then returns `null` instead when the length
is 0.
@safe ubyte* oops1(ubyte[] b) {
return b.ptr;
}
@safe ubyte oops2(ubyte[] b) {
return *b.ptr;
}
void main() {
auto b = new ubyte[42];
assert(oops1(b[0 .. $]) is &b[0]);
assert(oops1(b[0 .. 1]) is &b[0]);
assert(oops1(b[0 .. 0]) is null); // the 'safer' behavior
// With the proposed behavior, this call will cause null pointer
dereference,
// then it's deterministic and does not cause undefined behavior.
oops2(b[0 .. 0]);
}
--
More information about the Digitalmars-d-bugs
mailing list