Blame SOURCES/00200-CVE-2014-4616.patch

ae2451
ae2451
# HG changeset patch
ae2451
# User Benjamin Peterson <benjamin@python.org>
ae2451
# Date 1397441438 14400
ae2451
# Node ID 50c07ed1743da9cd4540d83de0c30bd17aeb41b0
ae2451
# Parent  218e28a935ab4494d05215c243e2129625a71893
ae2451
in scan_once, prevent the reading of arbitrary memory when passed a negative index
ae2451
ae2451
Bug reported by Guido Vranken.
ae2451
ae2451
diff --git a/Lib/json/tests/test_decode.py b/Lib/json/tests/test_decode.py
ae2451
--- a/Lib/json/tests/test_decode.py
ae2451
+++ b/Lib/json/tests/test_decode.py
ae2451
@@ -60,5 +60,10 @@ class TestDecode(object):
ae2451
         msg = 'escape'
ae2451
         self.assertRaisesRegexp(ValueError, msg, self.loads, s)
ae2451
 
ae2451
+    def test_negative_index(self):
ae2451
+        d = self.json.JSONDecoder()
ae2451
+        self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
ae2451
+        self.assertRaises(ValueError, d.raw_decode, u'a'*42, -50000)
ae2451
+
ae2451
 class TestPyDecode(TestDecode, PyTest): pass
ae2451
 class TestCDecode(TestDecode, CTest): pass
ae2451
diff --git a/Modules/_json.c b/Modules/_json.c
ae2451
--- a/Modules/_json.c
ae2451
+++ b/Modules/_json.c
ae2451
@@ -1468,7 +1468,10 @@ scan_once_str(PyScannerObject *s, PyObje
ae2451
     PyObject *res;
ae2451
     char *str = PyString_AS_STRING(pystr);
ae2451
     Py_ssize_t length = PyString_GET_SIZE(pystr);
ae2451
-    if (idx >= length) {
ae2451
+    if (idx < 0)
ae2451
+        /* Compatibility with the Python version. */
ae2451
+        idx += length;
ae2451
+    if (idx < 0 || idx >= length) {
ae2451
         PyErr_SetNone(PyExc_StopIteration);
ae2451
         return NULL;
ae2451
     }
ae2451
@@ -1555,7 +1558,10 @@ scan_once_unicode(PyScannerObject *s, Py
ae2451
     PyObject *res;
ae2451
     Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
ae2451
     Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
ae2451
-    if (idx >= length) {
ae2451
+    if (idx < 0)
ae2451
+        /* Compatibility with Python version. */
ae2451
+        idx += length;
ae2451
+    if (idx < 0 || idx >= length) {
ae2451
         PyErr_SetNone(PyExc_StopIteration);
ae2451
         return NULL;
ae2451
     }
ae2451