Thread overview
[Issue 890] New: Returning char[4] and assigning to char[] produces unexpected results.
Jan 26, 2007
d-bugmail
Jan 26, 2007
d-bugmail
Jan 26, 2007
d-bugmail
Feb 12, 2007
d-bugmail
January 26, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=890

           Summary: Returning char[4] and assigning to char[] produces
                    unexpected results.
           Product: D
           Version: 1.001
          Platform: All
        OS/Version: Linux
            Status: NEW
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: bugzilla@digitalmars.com
        ReportedBy: d@jeffmcglynn.com


import std.stdio;

char[] get_str() {
        char[4] retVal = ['t', 'e', 's', 't'];
        return retVal;
}

void main() {
        char[] res = get_str();
        writefln(res == "test" ? "SUCCESS" : "FAIL");
}


Results:

GDC 0.21 Mac, gdc -o testcase_debug -fdebug -fbounds-check unique.d FAIL

GDC 0.21 Mac, gdc -o testcase -O3 -finline -fno-bounds-check unique.d SUCCESS

DMD 1.002 Linux, dmd -oftestcase_debug -debug unique.d
FAIL

DMD 1.002 Linux, dmd -oftestcase -O -release -inline unique.d SUCCESS

------

Attempting to output res results in "`_Error: 4invalid UTF-8 sequence" when this comparison fails.  This bug also has a weird bonus:  If the comparison is moved into a function it succeeds in debug builds, but attempting to output res results in the above UTF-8 error.


-- 

January 26, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=890


bugzilla@digitalmars.com changed:

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




------- Comment #1 from bugzilla@digitalmars.com  2007-01-26 03:49 -------
It fails because statically dimensioned arrays, like char[4], are allocated on the stack, and the return statement is returning a reference to that stack variable. When the function returns, the stack variable becomes garbage.


-- 

January 26, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=890


benoit@tionex.de changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
           Severity|normal                      |enhancement
             Status|RESOLVED                    |REOPENED
         Resolution|INVALID                     |




------- Comment #2 from benoit@tionex.de  2007-01-26 04:27 -------
Please add an error for this. Probably it is not possible to catch all such
cases. But it should be possible for the trivial case: returning a reference to
local defined char[4].
This will prevent more ppl from having that problem.

I changed this item from bug to enhancement.


-- 

February 12, 2007
http://d.puremagic.com/issues/show_bug.cgi?id=890


bugzilla@digitalmars.com changed:

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




------- Comment #3 from bugzilla@digitalmars.com  2007-02-12 03:41 -------
Fixed DMD 1.005


--