[3.15] gh-152052: Fix misleading json error for \uXXXX escape at the end of input (GH-152053) (#152283)

(cherry picked from commit 588be7af08)

Co-authored-by: tonghuaroot (童话) <tonghuaroot@gmail.com>
Co-authored-by: Stan Ulbrych <stan@python.org>
This commit is contained in:
Miss Islington (bot) 2026-06-26 16:15:21 +02:00 committed by GitHub
parent a89de4b230
commit f3e6bdafd2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 13 additions and 1 deletions

View file

@ -144,6 +144,13 @@ def test_truncated_input(self):
('"', 'Unterminated string starting at', 0),
('"spam', 'Unterminated string starting at', 0),
]
# A complete \uXXXX escape at end of input leaves it unterminated.
test_cases += [
(r'"\u0041', 'Unterminated string starting at', 0),
(r'"\ud834', 'Unterminated string starting at', 0),
(r'"\ud834\udd1e', 'Unterminated string starting at', 0),
(r'{"a": "\u0041', 'Unterminated string starting at', 6),
]
for data, msg, idx in test_cases:
with self.assertRaises(self.JSONDecodeError) as cm:
self.loads(data)

View file

@ -137,6 +137,9 @@ def test_bad_escapes(self):
'"\\ud834\\u-123"',
'"\\ud834\\u+123"',
'"\\ud834\\u1_23"',
# Truncated or non-hex \uXXXX escape at end of input.
'"\\u004',
'"\\uXYZW',
]
for s in bad_escapes:
with self.assertRaises(self.JSONDecodeError, msg=s):

View file

@ -0,0 +1,2 @@
The :mod:`json` C accelerator now correctly reports an unterminated string for a
``\uXXXX`` escape at the end of the input.

View file

@ -574,7 +574,7 @@ scanstring_unicode(PyObject *pystr, Py_ssize_t end, int strict, Py_ssize_t *next
c = 0;
next++;
end = next + 4;
if (end >= len) {
if (end > len) {
raise_errmsg("Invalid \\uXXXX escape", pystr, next - 1);
goto bail;
}