[Python-checkins] cpython: Issue #16475 : Correctly handle the EOF when reading marshal streams.

kristjan.jonsson python-checkins at python.org
Wed Mar 20 23:01:42 CET 2013


http://hg.python.org/cpython/rev/42bf74b90626
changeset:   82848:42bf74b90626
user:        Kristján Valur Jónsson <sweskman at gmail.com>
date:        Wed Mar 20 14:26:33 2013 -0700
summary:
  Issue #16475 : Correctly handle the EOF when reading marshal streams.

files:
  Lib/test/test_marshal.py |   5 +++++
  Python/marshal.c         |  18 +++++++++---------
  2 files changed, 14 insertions(+), 9 deletions(-)


diff --git a/Lib/test/test_marshal.py b/Lib/test/test_marshal.py
--- a/Lib/test/test_marshal.py
+++ b/Lib/test/test_marshal.py
@@ -283,6 +283,11 @@
         unicode_string = 'T'
         self.assertRaises(TypeError, marshal.loads, unicode_string)
 
+    def _test_eof(self):
+        data = marshal.dumps(("hello", "dolly", None))
+        for i in range(len(data)):
+            self.assertRaises(EOFError, marshal.loads, data[0: i])
+
 LARGE_SIZE = 2**31
 pointer_size = 8 if sys.maxsize > 0xFFFFFFFF else 4
 
diff --git a/Python/marshal.c b/Python/marshal.c
--- a/Python/marshal.c
+++ b/Python/marshal.c
@@ -808,10 +808,16 @@
     PyObject *v, *v2;
     Py_ssize_t idx = 0;
     long i, n;
-    int type = r_byte(p);
+    int type, code = r_byte(p);
     int flag;
     PyObject *retval;
 
+    if (code == EOF) {
+        PyErr_SetString(PyExc_EOFError,
+                        "EOF read where object expected");
+        return NULL;
+    }
+
     p->depth++;
 
     if (p->depth > MAX_MARSHAL_STACK_DEPTH) {
@@ -820,8 +826,8 @@
         return NULL;
     }
 
-    flag = type & FLAG_REF;
-    type = type & ~FLAG_REF;
+    flag = code & FLAG_REF;
+    type = code & ~FLAG_REF;
 
 #define R_REF(O) do{\
     if (flag) \
@@ -830,12 +836,6 @@
 
     switch (type) {
 
-    case EOF:
-        PyErr_SetString(PyExc_EOFError,
-                        "EOF read where object expected");
-        retval = NULL;
-        break;
-
     case TYPE_NULL:
         retval = NULL;
         break;

-- 
Repository URL: http://hg.python.org/cpython


More information about the Python-checkins mailing list