diff options
author | Ezio Melotti <none@none> | 2011-04-11 00:44:28 (GMT) |
---|---|---|
committer | Ezio Melotti <none@none> | 2011-04-11 00:44:28 (GMT) |
commit | 8b4367ec10c0518b06a8291f4313ef7677d85988 (patch) | |
tree | 9e902463f32196131c44779e49756f688b7f2aed /Modules/pyexpat.c | |
parent | 7dfc874a48516e58e830b3a455fca34c8cd834eb (diff) | |
download | cpython-8b4367ec10c0518b06a8291f4313ef7677d85988.zip cpython-8b4367ec10c0518b06a8291f4313ef7677d85988.tar.gz cpython-8b4367ec10c0518b06a8291f4313ef7677d85988.tar.bz2 |
#4877: Fix a segfault in xml.parsers.expat while attempting to parse a closed file.
Diffstat (limited to 'Modules/pyexpat.c')
-rw-r--r-- | Modules/pyexpat.c | 38 |
1 files changed, 12 insertions, 26 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 0555b6d..af85582 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -962,21 +962,15 @@ static PyObject * xmlparse_ParseFile(xmlparseobject *self, PyObject *f) { int rv = 1; - FILE *fp; PyObject *readmethod = NULL; - if (PyFile_Check(f)) { - fp = PyFile_AsFile(f); - } - else { - fp = NULL; - readmethod = PyObject_GetAttrString(f, "read"); - if (readmethod == NULL) { - PyErr_Clear(); - PyErr_SetString(PyExc_TypeError, - "argument must have 'read' attribute"); - return NULL; - } + readmethod = PyObject_GetAttrString(f, "read"); + if (readmethod == NULL) { + PyErr_Clear(); + PyErr_SetString(PyExc_TypeError, + "argument must have 'read' attribute"); + return NULL; + } for (;;) { int bytes_read; @@ -986,20 +980,12 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f) return PyErr_NoMemory(); } - if (fp) { - bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp); - if (bytes_read < 0) { - PyErr_SetFromErrno(PyExc_IOError); - return NULL; - } - } - else { - bytes_read = readinst(buf, BUF_SIZE, readmethod); - if (bytes_read < 0) { - Py_XDECREF(readmethod); - return NULL; - } + bytes_read = readinst(buf, BUF_SIZE, readmethod); + if (bytes_read < 0) { + Py_XDECREF(readmethod); + return NULL; } + rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0); if (PyErr_Occurred()) { Py_XDECREF(readmethod); |