[Issue 24394] New: mutable array can be returned as string

d-bugmail at puremagic.com d-bugmail at puremagic.com
Wed Feb 14 02:06:12 UTC 2024


https://issues.dlang.org/show_bug.cgi?id=24394

          Issue ID: 24394
           Summary: mutable array can be returned as string
           Product: D
           Version: D2
          Hardware: x86
                OS: Windows
            Status: NEW
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: forestix at nom.one

Originally reported in the forum:

https://forum.dlang.org/thread/gkoqjxixfiwftivjkxqp@forum.dlang.org

I'm now reporting it here, since more experienced D users seem to agree it's a
bug.
I was surprised to find that this code compiles with no complaints:


@safe:

string test(ubyte[] arr)
{
    import std.string;
    return arr.assumeUTF; // why does this compile?
}

void main()
{
    import std.stdio;
    ubyte[] buf = ['g', 'o', 'o', 'd'];
    string dodgy = buf.test; // this string points to mutable data

    buf[] = ['b', 'a', 'd', '!'];
    writeln(typeof(dodgy).stringof);
    writeln(dodgy);
}

--


More information about the Digitalmars-d-bugs mailing list