mirror of
https://github.com/python/cpython.git
synced 2025-08-03 00:23:06 +00:00
#4877: Fix a segfault in xml.parsers.expat while attempting to parse a closed file.
This commit is contained in:
parent
7dfc874a48
commit
8b4367ec10
3 changed files with 27 additions and 27 deletions
|
@ -6,6 +6,7 @@ import unittest
|
||||||
|
|
||||||
from xml.parsers import expat
|
from xml.parsers import expat
|
||||||
|
|
||||||
|
from test import test_support
|
||||||
from test.test_support import sortdict, run_unittest
|
from test.test_support import sortdict, run_unittest
|
||||||
|
|
||||||
|
|
||||||
|
@ -217,6 +218,16 @@ class ParseTest(unittest.TestCase):
|
||||||
self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
|
self.assertEqual(op[15], "External entity ref: (None, u'entity.file', None)")
|
||||||
self.assertEqual(op[16], "End element: u'root'")
|
self.assertEqual(op[16], "End element: u'root'")
|
||||||
|
|
||||||
|
# Issue 4877: expat.ParseFile causes segfault on a closed file.
|
||||||
|
fp = open(test_support.TESTFN, 'wb')
|
||||||
|
try:
|
||||||
|
fp.close()
|
||||||
|
parser = expat.ParserCreate()
|
||||||
|
with self.assertRaises(ValueError):
|
||||||
|
parser.ParseFile(fp)
|
||||||
|
finally:
|
||||||
|
test_support.unlink(test_support.TESTFN)
|
||||||
|
|
||||||
|
|
||||||
class NamespaceSeparatorTest(unittest.TestCase):
|
class NamespaceSeparatorTest(unittest.TestCase):
|
||||||
def test_legal(self):
|
def test_legal(self):
|
||||||
|
|
|
@ -51,11 +51,14 @@ Core and Builtins
|
||||||
Library
|
Library
|
||||||
-------
|
-------
|
||||||
|
|
||||||
|
- Issue #4877: Fix a segfault in xml.parsers.expat while attempting to parse
|
||||||
|
a closed file.
|
||||||
|
|
||||||
- Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
|
- Issue #8428: Fix a race condition in multiprocessing.Pool when terminating
|
||||||
worker processes: new processes would be spawned while the pool is being
|
worker processes: new processes would be spawned while the pool is being
|
||||||
shut down. Patch by Charles-François Natali.
|
shut down. Patch by Charles-François Natali.
|
||||||
|
|
||||||
- Issue #7311: fix HTMLParser to accept non-ASCII attribute values.
|
- Issue #7311: Fix HTMLParser to accept non-ASCII attribute values.
|
||||||
|
|
||||||
- Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
|
- Issue #10963: Ensure that subprocess.communicate() never raises EPIPE.
|
||||||
|
|
||||||
|
|
|
@ -962,21 +962,15 @@ static PyObject *
|
||||||
xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
|
xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
|
||||||
{
|
{
|
||||||
int rv = 1;
|
int rv = 1;
|
||||||
FILE *fp;
|
|
||||||
PyObject *readmethod = NULL;
|
PyObject *readmethod = NULL;
|
||||||
|
|
||||||
if (PyFile_Check(f)) {
|
readmethod = PyObject_GetAttrString(f, "read");
|
||||||
fp = PyFile_AsFile(f);
|
if (readmethod == NULL) {
|
||||||
}
|
PyErr_Clear();
|
||||||
else {
|
PyErr_SetString(PyExc_TypeError,
|
||||||
fp = NULL;
|
"argument must have 'read' attribute");
|
||||||
readmethod = PyObject_GetAttrString(f, "read");
|
return NULL;
|
||||||
if (readmethod == NULL) {
|
|
||||||
PyErr_Clear();
|
|
||||||
PyErr_SetString(PyExc_TypeError,
|
|
||||||
"argument must have 'read' attribute");
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
for (;;) {
|
for (;;) {
|
||||||
int bytes_read;
|
int bytes_read;
|
||||||
|
@ -986,20 +980,12 @@ xmlparse_ParseFile(xmlparseobject *self, PyObject *f)
|
||||||
return PyErr_NoMemory();
|
return PyErr_NoMemory();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (fp) {
|
bytes_read = readinst(buf, BUF_SIZE, readmethod);
|
||||||
bytes_read = fread(buf, sizeof(char), BUF_SIZE, fp);
|
if (bytes_read < 0) {
|
||||||
if (bytes_read < 0) {
|
Py_XDECREF(readmethod);
|
||||||
PyErr_SetFromErrno(PyExc_IOError);
|
return NULL;
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
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);
|
rv = XML_ParseBuffer(self->itself, bytes_read, bytes_read == 0);
|
||||||
if (PyErr_Occurred()) {
|
if (PyErr_Occurred()) {
|
||||||
Py_XDECREF(readmethod);
|
Py_XDECREF(readmethod);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue