From 59c779db0e003adaa3e6e98021bc61fc205e61d1 Mon Sep 17 00:00:00 2001 From: Victor Stinner Date: Thu, 14 Jan 2010 19:41:48 +0100 Subject: [PATCH] issue 3299: replace PyObject_DEL() by Py_DECREF() in pyexpat module The bug is raised on malloc() failure. Add also a dummy test on ExternalEntityParserCreate(). --- Lib/test/test_pyexpat.py | 20 ++++++++++++++++++++ Modules/pyexpat.c | 23 ++++++++--------------- 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/Lib/test/test_pyexpat.py b/Lib/test/test_pyexpat.py index 649d5d2..c79960a 100644 --- a/Lib/test/test_pyexpat.py +++ b/Lib/test/test_pyexpat.py @@ -218,6 +218,26 @@ class ParseTest(unittest.TestCase): self.assertEquals(op[15], "External entity ref: (None, u'entity.file', None)") self.assertEquals(op[16], "End element: u'root'") + def test_issue3299(self): + # create an ExternalEntityParserCreate with buffer text + class ExternalOutputter: + def __init__(self, parser): + self.parser = parser + self.parser_result = None + + def ExternalEntityRefHandler(self, context, base, sysId, pubId): + external_parser = self.parser.ExternalEntityParserCreate("") + self.parser_result = external_parser.Parse("", 1) + return 1 + + parser = expat.ParserCreate(namespace_separator='!') + parser.buffer_text = 1 + parser.returns_unicode = 1 + out = ExternalOutputter(parser) + parser.ExternalEntityRefHandler = out.ExternalEntityRefHandler + parser.Parse(data, 1) + self.assertEquals(out.parser_result, 1) + class NamespaceSeparatorTest(unittest.TestCase): def test_legal(self): diff --git a/Modules/pyexpat.c b/Modules/pyexpat.c index 47ef186..1fc5aa4 100644 --- a/Modules/pyexpat.c +++ b/Modules/pyexpat.c @@ -1092,21 +1092,7 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) return NULL; new_parser->buffer_size = self->buffer_size; new_parser->buffer_used = 0; - if (self->buffer != NULL) { - new_parser->buffer = malloc(new_parser->buffer_size); - if (new_parser->buffer == NULL) { -#ifndef Py_TPFLAGS_HAVE_GC - /* Code for versions 2.0 and 2.1 */ - PyObject_Del(new_parser); -#else - /* Code for versions 2.2 and later. */ - PyObject_GC_Del(new_parser); -#endif - return PyErr_NoMemory(); - } - } - else - new_parser->buffer = NULL; + new_parser->buffer = NULL; new_parser->returns_unicode = self->returns_unicode; new_parser->ordered_attributes = self->ordered_attributes; new_parser->specified_attributes = self->specified_attributes; @@ -1123,6 +1109,13 @@ xmlparse_ExternalEntityParserCreate(xmlparseobject *self, PyObject *args) PyObject_GC_Init(new_parser); #endif + if (self->buffer != NULL) { + new_parser->buffer = malloc(new_parser->buffer_size); + if (new_parser->buffer == NULL) { + Py_DECREF(new_parser); + return PyErr_NoMemory(); + } + } if (!new_parser->itself) { Py_DECREF(new_parser); return PyErr_NoMemory(); -- 1.6.6