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.
This commit is contained in:
Victor Stinner 2011-01-10 23:00:36 +00:00
parent 9971e001ef
commit 95f1dfc955
1 changed files with 12 additions and 22 deletions

View File

@ -797,25 +797,13 @@ xmlparse_Parse(xmlparseobject *self, PyObject *args)
static int static int
readinst(char *buf, int buf_size, PyObject *meth) readinst(char *buf, int buf_size, PyObject *meth)
{ {
PyObject *arg = NULL; PyObject *str;
PyObject *bytes = NULL; Py_ssize_t len;
PyObject *str = NULL;
Py_ssize_t len = -1;
char *ptr; char *ptr;
if ((bytes = PyLong_FromLong(buf_size)) == NULL) str = PyObject_CallFunction(meth, "n", buf_size);
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);
if (str == NULL) if (str == NULL)
goto finally; goto error;
if (PyBytes_Check(str)) if (PyBytes_Check(str))
ptr = PyBytes_AS_STRING(str); ptr = PyBytes_AS_STRING(str);
@ -825,7 +813,7 @@ readinst(char *buf, int buf_size, PyObject *meth)
PyErr_Format(PyExc_TypeError, PyErr_Format(PyExc_TypeError,
"read() did not return a bytes object (type=%.400s)", "read() did not return a bytes object (type=%.400s)",
Py_TYPE(str)->tp_name); Py_TYPE(str)->tp_name);
goto finally; goto error;
} }
len = Py_SIZE(str); len = Py_SIZE(str);
if (len > buf_size) { if (len > buf_size) {
@ -833,14 +821,16 @@ readinst(char *buf, int buf_size, PyObject *meth)
"read() returned too much data: " "read() returned too much data: "
"%i bytes requested, %zd returned", "%i bytes requested, %zd returned",
buf_size, len); buf_size, len);
goto finally; goto error;
} }
memcpy(buf, ptr, len); memcpy(buf, ptr, len);
finally: Py_DECREF(str);
Py_XDECREF(arg); /* len <= buf_size <= INT_MAX */
Py_XDECREF(str);
/* len <= buf_size <= INT_MAX (see above) */
return (int)len; return (int)len;
error:
Py_XDECREF(str);
return -1;
} }
PyDoc_STRVAR(xmlparse_ParseFile__doc__, PyDoc_STRVAR(xmlparse_ParseFile__doc__,