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

Commit 70741bc

Browse files
Handle invalid code points
1 parent 599cac7 commit 70741bc

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/core/internal/dassert.d

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,12 +97,23 @@ private string miniFormat(V)(const scope ref V v)
9797
{
9898
static if (is(V == char))
9999
{
100-
return ['\'', v, '\''];
100+
// Avoid invalid code points
101+
if (v < 0x7F)
102+
return ['\'', v, '\''];
103+
104+
uint tmp = v;
105+
return "cast(char) " ~ miniFormat(tmp);
101106
}
102107
else static if (is(V == wchar) || is(V == dchar))
103108
{
104-
import core.internal.utf: toUTF8;
105-
return toUTF8(['\'', v, '\'']);
109+
import core.internal.utf: isValidDchar, toUTF8;
110+
111+
// Avoid invalid code points
112+
if (isValidDchar(v))
113+
return toUTF8(['\'', v, '\'']);
114+
115+
uint tmp = v;
116+
return "cast(" ~ V.stringof ~ ") " ~ miniFormat(tmp);
106117
}
107118
else
108119
{

test/exceptions/src/assert_fail.d

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,11 @@ void testStrings()
6767
test('A', 'B', "'A' != 'B'");
6868
test(wchar(''), wchar(''), "'❤' != '∑'");
6969
test(dchar(''), dchar(''), "'❤' != '∑'");
70+
71+
// Detect invalid code points
72+
test(char(255), 'B', "cast(char) 255 != 'B'");
73+
test(wchar(0xD888), wchar(''), "cast(wchar) 55432 != '∑'");
74+
test(dchar(0xDDDD), dchar(''), "cast(dchar) 56797 != '∑'");
7075
}
7176

7277
void testToString()()

0 commit comments

Comments
 (0)