[Issue 17261] New: Implicit cast from static array to immutable should not be allowed
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Mar 16 10:25:51 PDT 2017
https://issues.dlang.org/show_bug.cgi?id=17261
Issue ID: 17261
Summary: Implicit cast from static array to immutable should
not be allowed
Product: D
Version: D2
Hardware: x86_64
OS: Linux
Status: NEW
Severity: normal
Priority: P1
Component: dmd
Assignee: nobody at puremagic.com
Reporter: hsteoh at quickfur.ath.cx
Code:
--------
import std.stdio;
char[32] func() {
char[32] staticArr = "A123456789abcdefB123456789abcdef"; // canary
return staticArr; // OK, by-value return
}
string gunk() {
string x = func(); // implicit conversion char[16] -> string
writeln(x.ptr);
writeln(x);
return x;
}
void main() {
auto s = gunk();
writeln(s.ptr);
writeln(s);
}
--------
Output:
--------
7FFCD89895C0
A123456789abcdefB123456789abcdef
7FFCD89895C0
9abcdef ����
--------
Basically, func() returns a static array by value, so it resides on the stack.
The implicit conversion in gunk() produces a slice of this stack data (red
flag), which gets corrupted (overwritten) when it returns to main().
Implicitly casting static arrays to immutable should not be allowed.
--
More information about the Digitalmars-d-bugs
mailing list