diff options
author | Victor Stinner <victor.stinner@haypocalc.com> | 2011-01-10 23:00:36 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@haypocalc.com> | 2011-01-10 23:00:36 (GMT) |
commit | 95f1dfc955310c86398fbb430274ec36aa0daad4 (patch) | |
tree | 11ef7d63060df0710f97800f0fb3c3eae911c5d7 /Modules | |
parent | 9971e001ef3aa41aa86528a36e038b1659c753c9 (diff) | |
download | cpython-95f1dfc955310c86398fbb430274ec36aa0daad4.zip cpython-95f1dfc955310c86398fbb430274ec36aa0daad4.tar.gz cpython-95f1dfc955310c86398fbb430274ec36aa0daad4.tar.bz2 |
Issue #9566: Fix pyparse.xmlparser.ParseFile()
Fix readinst() if file.read(n) returns a bytes object longer than n:
return -1 instead of the the buffer size to raise an exception.
Simplify also the function code.
Diffstat (limited to 'Modules')
-rw-r--r-- | Modules/pyexpat.c | 34 |
1 files changed, 12 insertions, 22 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index ae98ef7..ee59baf 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -797,25 +797,13 @@ xmlparse_Parse(xmlparseobject *self, PyObject *args) static int readinst(char *buf, int buf_size, PyObject *meth) { - PyObject *arg = NULL; - PyObject *bytes = NULL; - PyObject *str = NULL; - Py_ssize_t len = -1; + PyObject *str; + Py_ssize_t len; char *ptr; - if ((bytes = PyLong_FromLong(buf_size)) == NULL) - goto finally; - - if ((arg = PyTuple_New(1)) == NULL) { - Py_DECREF(bytes); - goto finally; - } - - PyTuple_SET_ITEM(arg, 0, bytes); - - str = PyObject_Call(meth, arg, NULL); + str = PyObject_CallFunction(meth, "n", buf_size); if (str == NULL) - goto finally; + goto error; if (PyBytes_Check(str)) ptr = PyBytes_AS_STRING(str); @@ -825,7 +813,7 @@ readinst(char *buf, int buf_size, PyObject *meth) PyErr_Format(PyExc_TypeError, "read() did not return a bytes object (type=%.400s)", Py_TYPE(str)->tp_name); - goto finally; + goto error; } len = Py_SIZE(str); if (len > buf_size) { @@ -833,14 +821,16 @@ readinst(char *buf, int buf_size, PyObject *meth) "read() returned too much data: " "%i bytes requested, %zd returned", buf_size, len); - goto finally; + goto error; } memcpy(buf, ptr, len); -finally: - Py_XDECREF(arg); - Py_XDECREF(str); - /* len <= buf_size <= INT_MAX (see above) */ + Py_DECREF(str); + /* len <= buf_size <= INT_MAX */ return (int)len; + +error: + Py_XDECREF(str); + return -1; } PyDoc_STRVAR(xmlparse_ParseFile__doc__, |