www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 627] New: Concatenation of strings to string arrays with ~ corrupts data

reply d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=627

           Summary: Concatenation of strings to string arrays with ~
                    corrupts data
           Product: D
           Version: 0.175
          Platform: PC
        OS/Version: Windows
            Status: NEW
          Keywords: wrong-code
          Severity: major
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla digitalmars.com
        ReportedBy: deewiant gmail.com


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);
}


-- 
Dec 02 2006
next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=627





------- Comment #1 from lio lunesu.com  2007-01-29 03:03 -------
//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.


-- 
Jan 29 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=627





------- Comment #2 from lio lunesu.com  2007-02-06 02:17 -------
(Wait, there's no AA. What was I thinking? Sorry 'bout that.)


-- 
Feb 06 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=627


bugzilla digitalmars.com changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
         Resolution|                            |FIXED




------- Comment #3 from bugzilla digitalmars.com  2007-07-01 13:26 -------
Fixed DMD 1.018 and DMD 2.002


-- 
Jul 01 2007
prev sibling parent d-bugmail puremagic.com writes:
http://d.puremagic.com/issues/show_bug.cgi?id=627





------- Comment #4 from thomas-dloop kuehne.cn  2007-07-23 14:57 -------
Added to DStress as
http://dstress.kuehne.cn/run/o/opCat_27_A.d
http://dstress.kuehne.cn/run/o/opCat_27_B.d
http://dstress.kuehne.cn/run/o/opCat_27_C.d


-- 
Jul 23 2007