www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 12013] New: static array of chars implicitly converts to string

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

           Summary: static array of chars implicitly converts to string
           Product: D
           Version: D2
          Platform: All
        OS/Version: All
            Status: NEW
          Keywords: accepts-invalid
          Severity: normal
          Priority: P2
         Component: DMD
        AssignedTo: nobody puremagic.com
        ReportedBy: destructionator gmail.com


--- Comment #0 from Adam D. Ruppe <destructionator gmail.com> 2014-01-27
13:03:36 PST ---
// note: this is NOT pure, so it isn't the pure != unique bug
char[8] test() {
    char[8] tmp;
    tmp[0 .. 8] = "deadbeef"; // fill it with something printable
    return tmp;
}

string wtf() {
    return test(); // this isn't sane!
}

void main() {
    string a = wtf();
    assert(0, a); // prints garbage; it points to reused stack data
}


This also affects Phobos std.digest, which returns a static array of chars with
toHexString.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2014
next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12013



--- Comment #1 from Adam D. Ruppe <destructionator gmail.com> 2014-01-27
13:04:36 PST ---
I tested btw on linux

DMD32 D Compiler v2.064

(It is the most recent released (non-beta) zip.)

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12013


bearophile_hugs eml.cc changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |bearophile_hugs eml.cc


--- Comment #2 from bearophile_hugs eml.cc 2014-01-27 13:14:26 PST ---
It's not just a problem with char[]/string, it's a more general problem (Rust
language has a type system designed to prevent such bugs statically):


int[6] foo() {
    return [10, 20, 30, 40, 50, 60];
}
int[] bar() {
    return foo;
}
void main() {
    import std.stdio;
    bar.writeln;
}


One output:

[4202649, 10, 6, 1244636, 1244668, 4202623]

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2014
prev sibling next sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12013



--- Comment #3 from Adam D. Ruppe <destructionator gmail.com> 2014-01-27
13:45:59 PST ---
The D language also has a type system that should catch such problems
statically... but I think this is three bugs coming together:

1) static arrays are implicitly sliced. i think this is a mistake since it
doesn't let you easily keep track of where the references are going. It is easy
enough to do [] when you want one.

2) it is an implicit cast from mutable to immutable. Generally ok since static
arrays are value types, but when combined with the implicit slice it is a
problem.

3) it is an escaping reference to stack data. This isn't supposed to be allowed
in D I think.


char[8] => string alone should be illegal

but it might do:

char[8] => immutable(char[8]), which makes sense alone in the same way int =>
immutable(int) is logical

but then auto slice it, immutable(char[8]) => immutable(char)[]... and now
we've entered insanity.

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 27 2014
prev sibling parent d-bugmail puremagic.com writes:
https://d.puremagic.com/issues/show_bug.cgi?id=12013


yebblies <yebblies gmail.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
             Status|NEW                         |RESOLVED
                 CC|                            |yebblies gmail.com
         Resolution|                            |DUPLICATE


--- Comment #4 from yebblies <yebblies gmail.com> 2014-01-28 20:38:24 EST ---
*** This issue has been marked as a duplicate of issue 9279 ***

-- 
Configure issuemail: https://d.puremagic.com/issues/userprefs.cgi?tab=email
------- You are receiving this mail because: -------
Jan 28 2014