Commit 41233d9
fix(json): reject raw unpaired surrogates in string lexing instead of aborting
Fixes #4049.
parse aborted (panic, not ParseError) on strings mixing a raw lone
trailing surrogate with any escape sequence, e.g. the 5-code-unit text
" U+DC00 \n ": lex_string_slow's flush sliced with the checked
ctx.input[start:end], which aborts when the code unit at a slice
boundary is a trailing surrogate. Meanwhile the escape-free fast path
silently *accepted* raw lone surrogates, producing ill-formed strings.
Per the design rule that MoonBit Strings stay Unicode well-formed
(unsafe_to_char is indeed unsafe), well-formed input cannot contain a
raw lone surrogate in the first place — but unsafe code can manufacture
such a String, and robustness demands a clean error over a process
abort or an ill-formed result. The string lexer now rejects raw
unpaired surrogates with the documented ParseError (InvalidChar) on
both the fast path and the slow path, and flush slices with the
bounds-check-only view(start_offset~, end_offset~) so no abort path
remains.
Escaped surrogate sequences (\uXXXX) are out of scope here; unpaired
escape handling is tracked in #4062 and fixed separately.
Deterministic regression tests in lex_string_test.mbt cover escape-free
and escape-adjacent raw lone surrogates (the former abort), the exact
ParseError shape, and still-accepted well-formed pairs.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent e4a037e commit 41233d9
2 files changed
Lines changed: 72 additions & 5 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
21 | 24 | | |
22 | 25 | | |
23 | 26 | | |
| |||
29 | 32 | | |
30 | 33 | | |
31 | 34 | | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
32 | 50 | | |
| 51 | + | |
33 | 52 | | |
34 | 53 | | |
35 | 54 | | |
| |||
39 | 58 | | |
40 | 59 | | |
41 | 60 | | |
42 | | - | |
43 | | - | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
44 | 69 | | |
45 | 70 | | |
46 | 71 | | |
| |||
70 | 95 | | |
71 | 96 | | |
72 | 97 | | |
73 | | - | |
74 | | - | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
75 | 107 | | |
76 | 108 | | |
77 | 109 | | |
78 | 110 | | |
| 111 | + | |
79 | 112 | | |
80 | 113 | | |
81 | 114 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
89 | 89 | | |
90 | 90 | | |
91 | 91 | | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
0 commit comments