Index: Python/marshal.c =================================================================== --- Python/marshal.c (revision 75133) +++ Python/marshal.c (working copy) @@ -589,7 +589,8 @@ ob->ob_size = n; for (i = 0; i < size; i++) { int digit = r_short(p); - if (digit < 0) { + if (digit < 0 || + (digit == 0 && i == size-1)) { Py_DECREF(ob); PyErr_SetString(PyExc_ValueError, "bad marshal data"); Index: Lib/test/test_marshal.py =================================================================== --- Lib/test/test_marshal.py (revision 75133) +++ Lib/test/test_marshal.py (working copy) @@ -262,7 +262,12 @@ testString = 'abc' * size marshal.dumps(testString) + def test_invalid_longs(self): + # Issue #7019: marshal.loads shouldn't produce unnormalized PyLongs + invalid_string = 'l\x02\x00\x00\x00\x00\x00\x00\x00' + self.assertRaises(ValueError, marshal.loads, invalid_string) + def test_main(): test_support.run_unittest(IntTestCase, FloatTestCase,