diff options
Diffstat (limited to 'Modules/pyexpat.c')
-rw-r--r-- | Modules/pyexpat.c | 64 |
1 files changed, 41 insertions, 23 deletions
diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 7e51d35..156dbf1 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -10,6 +10,9 @@ #define FIX_TRACE +static XML_Memory_Handling_Suite ExpatMemoryHandler = { + PyObject_Malloc, PyObject_Realloc, PyObject_Free}; + enum HandlerTypes { StartElement, EndElement, @@ -404,6 +407,10 @@ static void my_CharacterDataHandler(void *userData, const XML_Char *data, int len) { xmlparseobject *self = (xmlparseobject *) userData; + + if (PyErr_Occurred()) + return; + if (self->buffer == NULL) call_character_handler(self, data, len); else { @@ -438,6 +445,9 @@ my_StartElementHandler(void *userData, PyObject *container, *rv, *args; int i, max; + if (PyErr_Occurred()) + return; + if (flush_character_buffer(self) < 0) return; /* Set max to the number of slots filled in atts[]; max/2 is @@ -521,6 +531,8 @@ my_##NAME##Handler PARAMS {\ INIT \ \ if (have_handler(self, NAME)) { \ + if (PyErr_Occurred()) \ + return RETURN; \ if (flush_character_buffer(self) < 0) \ return RETURN; \ args = Py_BuildValue PARAM_FORMAT ;\ @@ -635,6 +647,9 @@ my_ElementDeclHandler(void *userData, PyObject *rv = NULL; PyObject *modelobj, *nameobj; + if (PyErr_Occurred()) + return; + if (flush_character_buffer(self) < 0) goto finally; modelobj = conv_content_model(model, (conv_string_to_unicode)); @@ -1002,7 +1017,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) PyObject_GC_Track(new_parser); if (self->buffer != NULL) { - new_parser->buffer = malloc(new_parser->buffer_size); + new_parser->buffer = PyMem_Malloc(new_parser->buffer_size); if (new_parser->buffer == NULL) { Py_DECREF(new_parser); return PyErr_NoMemory(); @@ -1019,7 +1034,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) for (i = 0; handler_info[i].name != NULL; i++) /* do nothing */; - new_parser->handlers = malloc(sizeof(PyObject *) * i); + new_parser->handlers = PyMem_Malloc(sizeof(PyObject *) * i); if (!new_parser->handlers) { Py_DECREF(new_parser); return PyErr_NoMemory(); @@ -1127,14 +1142,19 @@ PyUnknownEncodingHandler(void *encodingHandlerData, void *data; unsigned int kind; + if (PyErr_Occurred()) + return XML_STATUS_ERROR; + if (template_buffer[1] == 0) { for (i = 0; i < 256; i++) template_buffer[i] = i; } u = PyUnicode_Decode((char*) template_buffer, 256, name, "replace"); - if (u == NULL || PyUnicode_READY(u)) + if (u == NULL || PyUnicode_READY(u)) { + Py_XDECREF(u); return XML_STATUS_ERROR; + } if (PyUnicode_GET_LENGTH(u) != 256) { Py_DECREF(u); @@ -1180,28 +1200,26 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern) self->in_callback = 0; self->ns_prefixes = 0; self->handlers = NULL; - if (namespace_separator != NULL) { - self->itself = XML_ParserCreateNS(encoding, *namespace_separator); - } - else { - self->itself = XML_ParserCreate(encoding); - } -#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT) - /* This feature was added upstream in libexpat 2.1.0. Our expat copy - * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT - * to indicate that we can still use it. */ - XML_SetHashSalt(self->itself, - (unsigned long)_Py_HashSecret.prefix); -#endif self->intern = intern; Py_XINCREF(self->intern); PyObject_GC_Track(self); + + /* namespace_separator is either NULL or contains one char + \0 */ + self->itself = XML_ParserCreate_MM(encoding, &ExpatMemoryHandler, + namespace_separator); if (self->itself == NULL) { PyErr_SetString(PyExc_RuntimeError, "XML_ParserCreate failed"); Py_DECREF(self); return NULL; } +#if ((XML_MAJOR_VERSION >= 2) && (XML_MINOR_VERSION >= 1)) || defined(XML_HAS_SET_HASH_SALT) + /* This feature was added upstream in libexpat 2.1.0. Our expat copy + * has a backport of this feature where we also define XML_HAS_SET_HASH_SALT + * to indicate that we can still use it. */ + XML_SetHashSalt(self->itself, + (unsigned long)_Py_HashSecret.prefix); +#endif XML_SetUserData(self->itself, (void *)self); XML_SetUnknownEncodingHandler(self->itself, (XML_UnknownEncodingHandler) PyUnknownEncodingHandler, NULL); @@ -1209,7 +1227,7 @@ newxmlparseobject(char *encoding, char *namespace_separator, PyObject *intern) for (i = 0; handler_info[i].name != NULL; i++) /* do nothing */; - self->handlers = malloc(sizeof(PyObject *) * i); + self->handlers = PyMem_Malloc(sizeof(PyObject *) * i); if (!self->handlers) { Py_DECREF(self); return PyErr_NoMemory(); @@ -1236,11 +1254,11 @@ xmlparse_dealloc(xmlparseobject *self) self->handlers[i] = NULL; Py_XDECREF(temp); } - free(self->handlers); + PyMem_Free(self->handlers); self->handlers = NULL; } if (self->buffer != NULL) { - free(self->buffer); + PyMem_Free(self->buffer); self->buffer = NULL; } Py_XDECREF(self->intern); @@ -1440,7 +1458,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) return -1; if (b) { if (self->buffer == NULL) { - self->buffer = malloc(self->buffer_size); + self->buffer = PyMem_Malloc(self->buffer_size); if (self->buffer == NULL) { PyErr_NoMemory(); return -1; @@ -1451,7 +1469,7 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) else if (self->buffer != NULL) { if (flush_character_buffer(self) < 0) return -1; - free(self->buffer); + PyMem_Free(self->buffer); self->buffer = NULL; } return 0; @@ -1513,9 +1531,9 @@ xmlparse_setattro(xmlparseobject *self, PyObject *name, PyObject *v) } } /* free existing buffer */ - free(self->buffer); + PyMem_Free(self->buffer); } - self->buffer = malloc(new_buffer_size); + self->buffer = PyMem_Malloc(new_buffer_size); if (self->buffer == NULL) { PyErr_NoMemory(); return -1; |