Skip to content

Commit 66a3168

Browse files
committed
in scan_once, prevent the reading of arbitrary memory when passed a negative index
Bug reported by Guido Vranken.
1 parent 0c8cae1 commit 66a3168

File tree

4 files changed

+12
-1
lines changed

4 files changed

+12
-1
lines changed

Lib/json/tests/test_decode.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,9 @@ def test_decoder_optimizations(self):
4545
self.assertEqual(rval, {"key":"value", "k":"v"})
4646

4747

48+
def test_negative_index(self):
49+
d = self.json.JSONDecoder()
50+
self.assertRaises(ValueError, d.raw_decode, 'a'*42, -50000)
51+
4852
class TestPyDecode(TestDecode, PyTest): pass
4953
class TestCDecode(TestDecode, CTest): pass

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -842,6 +842,7 @@ Kannan Vijayan
842842
Kurt Vile
843843
Norman Vine
844844
Frank Visser
845+
Guido Vranken
845846
Niki W. Waibel
846847
Wojtek Walczak
847848
Charles Waldman

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ Core and Builtins
1313
Library
1414
-------
1515

16+
- Fix arbitrary memory access in JSONDecoder.raw_decode with a negative second
17+
parameter. Bug reported by Guido Vranken.
18+
1619
- Issue #20246: Fix buffer overflow in socket.recvfrom_into.
1720

1821
- Issue #19435: Fix directory traversal attack on CGIHttpRequestHandler.

Modules/_json.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -902,7 +902,10 @@ scan_once_unicode(PyScannerObject *s, PyObject *pystr, Py_ssize_t idx, Py_ssize_
902902
PyObject *res;
903903
Py_UNICODE *str = PyUnicode_AS_UNICODE(pystr);
904904
Py_ssize_t length = PyUnicode_GET_SIZE(pystr);
905-
if (idx >= length) {
905+
if (idx < 0)
906+
/* Compatibility with Python version. */
907+
idx += length;
908+
if (idx < 0 || idx >= length) {
906909
PyErr_SetNone(PyExc_StopIteration);
907910
return NULL;
908911
}

0 commit comments

Comments
 (0)