Bug 24394 – const(ubyte)[] array argument can be returned as string, mislabeling mutable data as immutable

Status
REOPENED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
x86
OS
All
Creation time
2024-02-14T02:06:12Z
Last change time
2024-12-13T19:33:19Z
Keywords
safe
Assigned to
No Owner
Creator
Forest
Moved to GitHub: dmd#20401 →

Comments

Comment #0 by forestix — 2024-02-14T02:06:12Z
Originally reported in the forum: https://forum.dlang.org/thread/[email protected] 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); }
Comment #1 by razvan.nitu1305 — 2024-02-14T10:58:25Z
This is fixed in master but the fix is behind a preview switch (-preview=fixImmutableConv).
Comment #2 by forestix — 2024-02-14T11:49:53Z
The above example does indeed trigger an error with -preview=fixImmutableConv on run.dlang.org. However, changing the test function's argument to const(ubyte)[] slips right past with no complaint. Shouldn't that be rejected as well? string test(const(ubyte)[] arr) { import std.string; return arr.assumeUTF; // why does this compile? }
Comment #3 by robert.schadek — 2024-12-13T19:33:19Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/dmd/issues/20401 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB