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

Skip to content

Commit 6168362

Browse files
committed
Issue #16475 : Correctly handle the EOF when reading marshal streams.
1 parent e178187 commit 6168362

2 files changed

Lines changed: 14 additions & 9 deletions

File tree

Lib/test/test_marshal.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -283,6 +283,11 @@ def test_loads_reject_unicode_strings(self):
283283
unicode_string = 'T'
284284
self.assertRaises(TypeError, marshal.loads, unicode_string)
285285

286+
def _test_eof(self):
287+
data = marshal.dumps(("hello", "dolly", None))
288+
for i in range(len(data)):
289+
self.assertRaises(EOFError, marshal.loads, data[0: i])
290+
286291
LARGE_SIZE = 2**31
287292
pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
288293

Python/marshal.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -808,10 +808,16 @@ r_object(RFILE *p)
808808
PyObject *v, *v2;
809809
Py_ssize_t idx = 0;
810810
long i, n;
811-
int type = r_byte(p);
811+
int type, code = r_byte(p);
812812
int flag;
813813
PyObject *retval;
814814

815+
if (code == EOF) {
816+
PyErr_SetString(PyExc_EOFError,
817+
"EOF read where object expected");
818+
return NULL;
819+
}
820+
815821
p->depth++;
816822

817823
if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
@@ -820,8 +826,8 @@ r_object(RFILE *p)
820826
return NULL;
821827
}
822828

823-
flag = type & FLAG_REF;
824-
type = type & ~FLAG_REF;
829+
flag = code & FLAG_REF;
830+
type = code & ~FLAG_REF;
825831

826832
#define R_REF(O) do{\
827833
if (flag) \
@@ -830,12 +836,6 @@ r_object(RFILE *p)
830836

831837
switch (type) {
832838

833-
case EOF:
834-
PyErr_SetString(PyExc_EOFError,
835-
"EOF read where object expected");
836-
retval = NULL;
837-
break;
838-
839839
case TYPE_NULL:
840840
retval = NULL;
841841
break;

0 commit comments

Comments
 (0)