Skip to content

Commit f3e6bda

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

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
@@ -144,6 +144,13 @@ def test_truncated_input(self):
144144
('"', 'Unterminated string starting at', 0),
145145
('"spam', 'Unterminated string starting at', 0),
146146
]
147+
# A complete \uXXXX escape at end of input leaves it unterminated.
148+
test_cases += [
149+
(r'"\u0041', 'Unterminated string starting at', 0),
150+
(r'"\ud834', 'Unterminated string starting at', 0),
151+
(r'"\ud834\udd1e', 'Unterminated string starting at', 0),
152+
(r'{"a": "\u0041', 'Unterminated string starting at', 6),
153+
]
147154
for data, msg, idx in test_cases:
148155
with self.assertRaises(self.JSONDecodeError) as cm:
149156
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
@@ -574,7 +574,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
574574
c = 0;
575575
next++;
576576
end = next + 4;
577-
if (end >= len) {
577+
if (end > len) {
578578
raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
579579
goto bail;
580580
}

0 commit comments

Comments
 (0)