Skip to content

Commit

Permalink
Fix bugzilla 24832 - Segfault in hex string (dlang/dmd!17024)
Browse files Browse the repository at this point in the history
* Fix bugzilla 24832 - Segfault in hex string

* Fix lack of camelcasing on stable branch
  • Loading branch information
dkorpel authored Oct 24, 2024
1 parent 24b56d8 commit ebafa4e
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion dmd/dcast.d
Original file line number Diff line number Diff line change
Expand Up @@ -2336,7 +2336,7 @@ Expression castTo(Expression e, Scope* sc, Type t, Type att = null)
Type tb = t.toBasetype();
Type typeb = e.type.toBasetype();

if (e.hexString && !e.committed)
if (e.hexString && !e.committed && tb.nextOf().isintegral)
{
const szx = cast(ubyte) tb.nextOf().size();
if (szx != se.sz && (e.len % szx) == 0)
Expand Down
2 changes: 1 addition & 1 deletion dmd/dinterpret.d
Original file line number Diff line number Diff line change
Expand Up @@ -6107,7 +6107,7 @@ public:
{
auto se = e1.isStringExp();
// Allow casting a hex string literal to short[], int[] or long[]
if (se && se.hexString && se.postfix == StringExp.NoPostfix)
if (se && se.hexString && se.postfix == StringExp.NoPostfix && e.to.nextOf().isintegral)
{
const sz = cast(size_t) e.to.nextOf().size;
if ((se.len % sz) != 0)
Expand Down
10 changes: 9 additions & 1 deletion tests/dmd/fail_compilation/hexstring.d
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,14 @@ fail_compilation/hexstring.d(39): perhaps remove postfix `c` from hex str
fail_compilation/hexstring.d(40): Error: hex string with `dstring` type needs to be multiple of 4 bytes, not 5
fail_compilation/hexstring.d(41): Error: cannot implicitly convert expression `x"11223344"d` of type `dstring` to `immutable(float[])`
fail_compilation/hexstring.d(42): Error: cannot implicitly convert expression `x"1122"w` of type `wstring` to `immutable(ubyte[])`
fail_compilation/hexstring.d(50): Error: array cast from `string` to `S[]` is not supported at compile time
fail_compilation/hexstring.d(28): Error: cannot implicitly convert expression `x"123F"` of type `string` to `ubyte[]`
---
*/
immutable ubyte[] s0 = x"123F";
static assert(s0[0] == 0x12);
static assert(s0[1] == 0x3F);
immutable byte[] s1 = x"123F";

enum E(X) = cast(X[]) x"AABBCCDD";
static assert(E!int[0] == 0xAABBCCDD);

Expand All @@ -40,3 +40,11 @@ immutable uint[] f11 = cast(immutable uint[]) x"AABBCCDD"c;
immutable uint[] f12 = x"1122334455"d;
immutable float[] f13 = x"11223344"d;
immutable ubyte[] f14 = x"1122"w;

// https://issues.dlang.org/show_bug.cgi?id=24832
struct S
{
ushort l0, l1, l2, l3, l4, l5;
}

immutable S[] returnValues = cast(S[]) x"FFFFFFFFFFFFFFFFFFFFFFFF";

0 comments on commit ebafa4e

Please sign in to comment.