Bug 627 – Concatenation of strings to string arrays with ~ corrupts data
Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P2
Component
dmd
Product
D
Version
D1 (retired)
Platform
x86
OS
Windows
Creation time
2006-12-02T13:52:00Z
Last change time
2014-02-15T13:22:04Z
Keywords
wrong-code
Assigned to
bugzilla
Creator
matti.niemenmaa+dbugzilla
Comments
Comment #0 by matti.niemenmaa+dbugzilla — 2006-12-02T13:52:14Z
void main() {
// it works with integers
int[] bar;
assert ((bar ~ 1).length == bar.length + 1);
// it works with integer arrays
int[][] baz;
assert ((baz ~ cast(int[])[1]).length == baz.length + 1);
// but not with string arrays, be they char[], wchar[], or dchar[]
char[][] foo;
assert ((foo ~ cast(char[])"foo").length == foo.length + 1);
// outputs 7303014 on my machine
printf("%d\n", (foo ~ cast(char[])"foo")[0].length);
// this works, though:
assert ((foo ~ [cast(char[])"foo"]).length == foo.length + 1);
// as does this:
char[] qux;
assert (([qux] ~ cast(char[])"foo").length == [qux].length + 1);
// and it works with literals - presumably constant folded?
assert (([cast(dchar[])"asdf"] ~ cast(dchar[])"foo").length == [cast(dchar[])"asdf"].length + 1);
// ~= works
char[][] quux;
auto quuux = quux.dup;
quuux ~= "foo";
assert (quuux.length == quux.length + 1);
}
Comment #1 by lio+bugzilla — 2007-01-29T03:03:32Z
//foo ~ cast(char[])"foo"
0x004020a9 6a08 push 08
0x004020ab ff358ce04000 push dword ptr [_TMP0+00000004 (0040e08c)]
0x004020b1 ff3588e04000 push dword ptr [_TMP0 (0040e088)]
0x004020b7 ff75dc push dword ptr [ebp-24]
0x004020ba ff75d8 push dword ptr [foo]
0x004020bd e8e6070000 call __d_arraycat (004028a8)
extern (C)
byte[] _d_arraycat(byte[] x, byte[] y, size_t size)
The "08" is the size of the data to be appended, char[].sizeof
Apparently, the AA is being treated as a normal array.
Comment #2 by lio+bugzilla — 2007-02-06T02:17:20Z
(Wait, there's no AA. What was I thinking? Sorry 'bout that.)