Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 99b5afa

Browse files
committed
in scan_once, prevent the reading of arbitrary memory when passed a negative index
Bug reported by Guido Vranken.
1 parent 80e6af1 commit 99b5afa

4 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/json_tests/test_decode.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,9 @@ def test_invalid_escape(self):
7070
msg = 'escape'
7171
self.assertRaisesRegex(ValueError, msg, self.loads, s)
7272

73+
def test_negative_index(self):
74+
d = self.json.JSONDecoder()
75+
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
76+
7377
class TestPyDecode(TestDecode, PyTest): pass
7478
class TestCDecode(TestDecode, CTest): pass

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1139,6 +1139,7 @@ Frank Visser
11391139
Johannes Vogel
11401140
Martijn Vries
11411141
Sjoerd de Vries
1142+
Guido Vranken
11421143
Niki W. Waibel
11431144
Wojtek Walczak
11441145
Charles Waldman

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,9 @@ What's New in Python 3.2.6?
1010
Library
1111
-------
1212

13+
- Fix arbitrary memory access in JSONDecoder.raw_decode with a negative second
14+
parameter. Bug reported by Guido Vranken.
15+
1316
- Issue #21082: In os.makedirs, do not set the process-wide umask. Note this
1417
changes behavior of makedirs when exist_ok=True.
1518

Modules/_json.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,10 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
930930
PyObject *res;
931931
Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
932932
Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
933-
if (idx >= length) {
933+
if (idx < 0)
934+
/* Compatibility with Python version. */
935+
idx += length;
936+
if (idx < 0 || idx >= length) {
934937
PyErr_SetNone(PyExc_StopIteration);
935938
return NULL;
936939
}

0 commit comments

Comments
 (0)