]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-152052: Fix misleading `json` error for `\uXXXX` escape at the end of input (...
authortonghuaroot (童话) <tonghuaroot@gmail.com>
Fri, 26 Jun 2026 13:47:22 +0000 (21:47 +0800)
committerGitHub <noreply@github.com>
Fri, 26 Jun 2026 13:47:22 +0000 (14:47 +0100)
Co-authored-by: Stan Ulbrych <stan@python.org>
Lib/test/test_json/test_fail.py
Lib/test/test_json/test_scanstring.py
Misc/NEWS.d/next/Library/2026-06-24-12-00-00.gh-issue-152052.yBssDE.rst [new file with mode: 0644]
Modules/_json.c

index 79c44af2fbf0e1f27e6b60556d423518f59bee70..7f99a6331d362519bfdaa16af115f596fb48b60e 100644 (file)
@@ -144,6 +144,13 @@ class TestFail:
             ('"', '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)
index 9a6cdfe12d266c0ef3944be099d394f22bb440bf..a52b01c71130d8c1efad3ec1531b4b7f736ac348 100644 (file)
@@ -137,6 +137,9 @@ class TestScanstring:
             '"\\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):
diff --git a/Misc/NEWS.d/next/Library/2026-06-24-12-00-00.gh-issue-152052.yBssDE.rst b/Misc/NEWS.d/next/Library/2026-06-24-12-00-00.gh-issue-152052.yBssDE.rst
new file mode 100644 (file)
index 0000000..f9ffbbd
--- /dev/null
@@ -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.
index b057b56b2f9f8d93234614f409efa6b8bbf7c71e..3a724a3e72b185b7d3a52a75103e3828cb5a4d18 100644 (file)
@@ -576,7 +576,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;
             }