www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 890] New: Returning char[4] and assigning to char[] produces unexpected results.

reply d-bugmail puremagic.com writes:
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.


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


bugzilla digitalmars.com changed:

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





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.


-- 
Jan 26 2007
prev sibling next sibling parent d-bugmail puremagic.com writes:
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                     |





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.


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


bugzilla digitalmars.com changed:

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





Fixed DMD 1.005


-- 
Feb 12 2007