Skip to content
This repository was archived by the owner on Oct 12, 2022. It is now read-only.

Commit a045839

Browse files
Fix Issue 20750 - checkaction=context segfaults for null references
1 parent ce26f60 commit a045839

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/core/internal/dassert.d

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,21 @@ private string miniFormat(V)(const ref V v)
9797
{
9898
return "`null`";
9999
}
100-
else static if (__traits(compiles, { string s = v.toString(); }))
101-
{
102-
return v.toString();
103-
}
104-
// Non-const toString(), e.g. classes inheriting from Object
100+
// toString() isn't always const, e.g. classes inheriting from Object
105101
else static if (__traits(compiles, { string s = V.init.toString(); }))
106102
{
107-
return (cast() v).toString();
103+
// Object references / struct pointers may be null
104+
static if (is(V == class) || is(V == interface) || is(V == U*, U))
105+
{
106+
if (v is null)
107+
return "`null`";
108+
}
109+
110+
// Prefer const overload of toString
111+
static if (__traits(compiles, { string s = v.toString(); }))
112+
return v.toString();
113+
else
114+
return (cast() v).toString();
108115
}
109116
// Static arrays or slices (but not aggregates with `alias this`)
110117
else static if (is(V : U[], U) && !isAggregateType!V)

test/exceptions/src/assert_fail.d

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ void testToString()()
9494
}
9595

9696
test!"!="(Overloaded(), Overloaded(), "Const == Const");
97+
98+
Foo fnull = null;
99+
test!"!is"(fnull, fnull, "`null` is `null`");
97100
}
98101

99102

0 commit comments

Comments
 (0)