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

Skip to content

Commit f2ca5af

Browse files
author
Michael W. Hudson
committed
Fix bug
[ 1180997 ] lax error-checking in new-in-2.4 marshal stuff which I'd assigned to Martin, but actually turned out to be easy to fix. Also, a test.
1 parent 01fca11 commit f2ca5af

2 files changed

Lines changed: 13 additions & 0 deletions

File tree

Lib/test/test_marshal.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,15 @@ def test_version_argument(self):
211211
self.assertEquals(marshal.loads(marshal.dumps(5, 0)), 5)
212212
self.assertEquals(marshal.loads(marshal.dumps(5, 1)), 5)
213213

214+
def test_fuzz(self):
215+
# simple test that it's at least not *totally* trivial to
216+
# crash from bad marshal data
217+
for c in [chr(i) for i in range(256)]:
218+
try:
219+
marshal.loads(c)
220+
except Exception:
221+
pass
222+
214223
def test_main():
215224
test_support.run_unittest(IntTestCase,
216225
FloatTestCase,

Python/marshal.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -648,6 +648,10 @@ r_object(RFILE *p)
648648

649649
case TYPE_STRINGREF:
650650
n = r_long(p);
651+
if (n < 0 || n >= PyList_GET_SIZE(p->strings)) {
652+
PyErr_SetString(PyExc_ValueError, "bad marshal data");
653+
return NULL;
654+
}
651655
v = PyList_GET_ITEM(p->strings, n);
652656
Py_INCREF(v);
653657
return v;

0 commit comments

Comments
 (0)