]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
in scan_once, prevent the reading of arbitrary memory when passed a negative index
authorBenjamin Peterson <benjamin@python.org>
Mon, 14 Apr 2014 02:10:38 +0000 (22:10 -0400)
committerBenjamin Peterson <benjamin@python.org>
Mon, 14 Apr 2014 02:10:38 +0000 (22:10 -0400)
Bug reported by Guido Vranken.

Lib/json/tests/test_decode.py
Misc/ACKS
Misc/NEWS
Modules/_json.c

index 144ff4113ce53214573586486ee4fbb9572d9993..a689b36779de0c11fe09014ee51c6534f8aea5c8 100644 (file)
@@ -45,5 +45,9 @@ class TestDecode:
         self.assertEqual(rval, {"key":"value", "k":"v"})
 
 
+    def test_negative_index(self):
+        d = self.json.JSONDecoder()
+        self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
+
 class TestPyDecode(TestDecode, PyTest): pass
 class TestCDecode(TestDecode, CTest): pass
index 0de41015b6def654237cd3d1893d21961cd5a771..6264d4b5011c613c41419d5bcbd9ff3a68067cfd 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -842,6 +842,7 @@ Kannan Vijayan
 Kurt Vile
 Norman Vine
 Frank Visser
+Guido Vranken
 Niki W. Waibel
 Wojtek Walczak
 Charles Waldman
index 437acbf33ffc1ba204c3be3c2335e61177ec2f66..68622f5299f76c24cea8ab2fbe4b5921417423d6 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -13,6 +13,9 @@ Core and Builtins
 Library
 -------
 
+- Fix arbitrary memory access in JSONDecoder.raw_decode with a negative second
+  parameter. Bug reported by Guido Vranken.
+
 - Issue #20246: Fix buffer overflow in socket.recvfrom_into.
 
 - Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.
index 5ced5c9704579dc081e593621e08b6ed184990dc..e54b0b94bc5001222ce85387d54c39a5e4305261 100644 (file)
@@ -902,7 +902,10 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
     PyObject *res;
     Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
     Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
-    if (idx >= length) {
+    if (idx < 0)
+        /* Compatibility with Python version. */
+        idx += length;
+    if (idx < 0 || idx >= length) {
         PyErr_SetNone(PyExc_StopIteration);
         return NULL;
     }