Issue 24337 - Segfault when printing an int[] cast from a string literal
Summary: Segfault when printing an int[] cast from a string literal
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 critical
Assignee: No Owner
URL:
Keywords: ice-on-valid-code, pull
Depends on:
Blocks:
 
Reported: 2024-01-12 12:12 UTC by Dennis
Modified: 2024-11-16 23:27 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description Dennis 2024-01-12 12:12:01 UTC
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.
Comment 1 Dlang Bot 2024-07-19 15:20:35 UTC
@dkorpel created dlang/dmd pull request #16729 "Fix bugzilla 24337 - Segfault when printing an int[] cast from a stri…" fixing this issue:

- Fix bugzilla 24337 - Segfault when printing an int[] cast from a string literal

https://github.com/dlang/dmd/pull/16729
Comment 2 Dlang Bot 2024-07-22 08:00:43 UTC
dlang/dmd pull request #16729 "Fix bugzilla 24337 - Segfault when printing an int[] cast from a stri…" was merged into stable:

- bb41c7e0e2264d2bcdcc63d6cfc2f089e5f3f1b8 by Dennis Korpel:
  Fix bugzilla 24337 - Segfault when printing an int[] cast from a string

https://github.com/dlang/dmd/pull/16729
Comment 3 Dlang Bot 2024-10-07 09:00:23 UTC
dlang/dmd pull request #16967 "Rebase stable6" was merged into master:

- 7e42effe7a5657d4443a7ea00e3509b889fd52b6 by Dennis:
  Fix bugzilla 24337 - Segfault when printing an int[] cast from a string (#16729)

https://github.com/dlang/dmd/pull/16967
Comment 4 Dlang Bot 2024-11-16 23:27:08 UTC
dlang/dmd pull request #17069 "Merge stable" was merged into master:

- cf618a659c92a0a6fcbc9079af29c72a8af27b76 by Dennis:
  Fix bugzilla 24337 - Segfault when printing an int[] cast from a string (#16729)

https://github.com/dlang/dmd/pull/17069