[Issue 17261] Implicit cast from static array to immutable should not be allowed
via Digitalmars-d-bugs
digitalmars-d-bugs at puremagic.com
Thu Mar 16 11:35:41 PDT 2017
https://issues.dlang.org/show_bug.cgi?id=17261
ZombineDev <petar.p.kirov at gmail.com> changed:
What |Removed |Added
----------------------------------------------------------------------------
Keywords| |safe
Status|NEW |RESOLVED
CC| |petar.p.kirov at gmail.com
Hardware|x86_64 |All
Resolution|--- |DUPLICATE
OS|Linux |All
--- Comment #6 from ZombineDev <petar.p.kirov at gmail.com> ---
(In reply to hsteoh from comment #0)
> 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.
After commenting-out the writeln calls I get:
scope_test2.d(12): Error: scope variable x may not be returned
when compiling with -dip1000 & DMD 2.073.2.
(Without commenting-out the writeln calls I still get a compiler error, but
it's because not all of Phobos is compatible with -dip1000.)
After replacing gunk with the one posted above:
string gunk() {
string x = func()[]; // still allowed, but shouldn't be
writeln(x.ptr);
writeln(x);
return x;
}
I get the same message:
scope_test3.d(12): Error: scope variable x may not be returned
*** This issue has been marked as a duplicate of issue 12625 ***
--
More information about the Digitalmars-d-bugs
mailing list