Commit ca31837
fix(json): reject 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), the string lexer now rejects every
unpaired surrogate with the documented ParseError (InvalidChar) instead
of either aborting or letting it through:
- raw lone leading/trailing surrogates are rejected on both the fast
path and the slow path;
- an escaped leading surrogate (\uD8xx) must be immediately followed by
an escaped trailing surrogate; the pair is combined into one scalar
value, anything else is rejected. BEHAVIOR CHANGE: "\uD800" alone
previously parsed into an ill-formed lone-surrogate string and is now
a parse error;
- well-formed pairs (raw or escaped) parse exactly as before;
- flush now slices with the bounds-check-only view(start_offset~,
end_offset~), so no abort path remains in string lexing.
Deterministic regression tests in lex_string_test.mbt cover every
rejection shape (raw, escaped, mixed raw/escaped halves) plus the
still-accepted well-formed pairs. Full repo suite green on wasm-gc; the
json suite green on wasm-gc, js, and native.
Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>1 parent d5a4518 commit ca31837
2 files changed
Lines changed: 115 additions & 6 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 | + | |
32 | 48 | | |
| 49 | + | |
33 | 50 | | |
34 | 51 | | |
35 | 52 | | |
| |||
39 | 56 | | |
40 | 57 | | |
41 | 58 | | |
42 | | - | |
43 | | - | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
44 | 68 | | |
45 | 69 | | |
46 | 70 | | |
| |||
63 | 87 | | |
64 | 88 | | |
65 | 89 | | |
66 | | - | |
| 90 | + | |
| 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 | + | |
67 | 120 | | |
68 | 121 | | |
69 | 122 | | |
70 | 123 | | |
71 | 124 | | |
72 | 125 | | |
73 | | - | |
74 | | - | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
75 | 134 | | |
76 | 135 | | |
77 | 136 | | |
78 | 137 | | |
| 138 | + | |
79 | 139 | | |
80 | 140 | | |
81 | 141 | | |
| |||
| 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 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
0 commit comments