Skip to content

Commit 03266f4

Browse files
committed
forbid reentrant calls to ParseFile()
1 parent ad69e1e commit 03266f4

1 file changed

Lines changed: 18 additions & 6 deletions

File tree

Modules/pyexpat.c

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -780,6 +780,18 @@ VOID_HANDLER(StartDoctypeDecl,
780780

781781
VOID_HANDLER(EndDoctypeDecl, (void *userData), ("()"))
782782

783+
/* check that the current function is not called from within a handler */
784+
#define CHECK_NOT_IN_HANDLER(PARSER, FUNCNAME) \
785+
do { \
786+
if (PARSER->in_callback) { \
787+
PyErr_SetString(PyExc_RuntimeError, \
788+
"cannot call " FUNCNAME "() " \
789+
"from within a handler"); \
790+
return NULL; \
791+
} \
792+
} while (0)
793+
794+
783795
/* ---------------------------------------------------------------- */
784796
/*[clinic input]
785797
class pyexpat.xmlparser "xmlparseobject *" "&Xmlparsetype"
@@ -857,18 +869,15 @@ pyexpat_xmlparser_Parse_impl(xmlparseobject *self, PyTypeObject *cls,
857869
PyObject *data, int isfinal)
858870
/*[clinic end generated code: output=8faffe07fe1f862a input=053e0f047e55c05a]*/
859871
{
872+
// avoid re-entrant calls to XML_Parse()
873+
CHECK_NOT_IN_HANDLER(self, "Parse");
874+
860875
const char *s;
861876
Py_ssize_t slen;
862877
Py_buffer view;
863878
int rc;
864879
pyexpat_state *state = PyType_GetModuleState(cls);
865880

866-
if (self->in_callback) {
867-
PyErr_SetString(PyExc_RuntimeError,
868-
"cannot call Parse() from within a handler");
869-
return NULL;
870-
}
871-
872881
if (PyUnicode_Check(data)) {
873882
view.buf = NULL;
874883
s = PyUnicode_AsUTF8AndSize(data, &slen);
@@ -962,6 +971,9 @@ pyexpat_xmlparser_ParseFile_impl(xmlparseobject *self, PyTypeObject *cls,
962971
PyObject *file)
963972
/*[clinic end generated code: output=34780a094c8ca3ae input=ba4bc9c541684793]*/
964973
{
974+
// avoid re-entrant calls to XML_GetBuffer() or XML_ParseBuffer()
975+
CHECK_NOT_IN_HANDLER(self, "ParseFile");
976+
965977
int rv = 1;
966978
PyObject *readmethod = NULL;
967979

0 commit comments

Comments
 (0)