Skip to content

Commit ac3b2ab

Browse files
miss-islingtontonghuarootStanFromIreland
authored
[3.13] gh-152052: Fix misleading json error for \uXXXX escape at the end of input (GH-152053) (#152285)
(cherry picked from commit 588be7a) Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com> Co-authored-by: Stan Ulbrych <stan@python.org>
1 parent 998587c commit ac3b2ab

4 files changed

Lines changed: 13 additions & 1 deletion

File tree

Lib/test/test_json/test_fail.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,13 @@ def test_truncated_input(self):
125125
('"', 'Unterminated string starting at', 0),
126126
('"spam', 'Unterminated string starting at', 0),
127127
]
128+
# A complete \uXXXX escape at end of input leaves it unterminated.
129+
test_cases += [
130+
(r'"\u0041', 'Unterminated string starting at', 0),
131+
(r'"\ud834', 'Unterminated string starting at', 0),
132+
(r'"\ud834\udd1e', 'Unterminated string starting at', 0),
133+
(r'{"a": "\u0041', 'Unterminated string starting at', 6),
134+
]
128135
for data, msg, idx in test_cases:
129136
with self.assertRaises(self.JSONDecodeError) as cm:
130137
self.loads(data)

Lib/test/test_json/test_scanstring.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ def test_bad_escapes(self):
137137
'"\\ud834\\u-123"',
138138
'"\\ud834\\u+123"',
139139
'"\\ud834\\u1_23"',
140+
# Truncated or non-hex \uXXXX escape at end of input.
141+
'"\\u004',
142+
'"\\uXYZW',
140143
]
141144
for s in bad_escapes:
142145
with self.assertRaises(self.JSONDecodeError, msg=s):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
The :mod:`json` C accelerator now correctly reports an unterminated string for a
2+
``\uXXXX`` escape at the end of the input.

Modules/_json.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
457457
c = 0;
458458
next++;
459459
end = next + 4;
460-
if (end >= len) {
460+
if (end > len) {
461461
raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
462462
goto bail;
463463
}

0 commit comments

Comments
 (0)