Blame 00196-json-add-boundary-check.patch

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