www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 24337] New: Segfault when printing an int[] cast from a

https://issues.dlang.org/show_bug.cgi?id=24337

          Issue ID: 24337
           Summary: Segfault when printing an int[] cast from a string
                    literal
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Keywords: ice-on-valid-code
          Severity: normal
          Priority: P1
         Component: dmd
          Assignee: nobody puremagic.com
          Reporter: dkorpel live.nl

This code segfaults dmd:
```D
immutable ushort[] z = cast(immutable ushort[]) "ab"w;
pragma(msg, z);
```

The problem is in expressionsem.d:

```
StringExp toUTF8(StringExp se, Scope* sc)
{
    if (se.sz != 1)
    {
        // Convert to UTF-8 string
        se.committed = false;
        Expression e = castTo(se, sc, Type.tchar.arrayOf());
        e = e.optimize(WANTvalue);
        auto result = e.isStringExp();
        assert(result.sz == 1);
        return result;
    }
    return se;
}
```

This function assumes casting a StringExp to char[] will always succeed, which
is false in the above case.

--
Jan 12