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

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