From 2cf6dafa3e95460a262162a9252fc6c7578a739b Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Tue, 5 Oct 2010 15:27:44 +0000 Subject: [PATCH] Handle null return from XML_ParserCreate and fix a few memory leaks on error conditions. Submitted by: Mark Johnston MFC after: 2 weeks --- lib/libgeom/geom_xml2tree.c | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/lib/libgeom/geom_xml2tree.c b/lib/libgeom/geom_xml2tree.c index 821449b66ca8..24315cda8c25 100644 --- a/lib/libgeom/geom_xml2tree.c +++ b/lib/libgeom/geom_xml2tree.c @@ -339,21 +339,27 @@ geom_xml2tree(struct gmesh *gmp, char *p) memset(gmp, 0, sizeof *gmp); LIST_INIT(&gmp->lg_class); parser = XML_ParserCreate(NULL); - mt = calloc(1, sizeof *mt); - if (mt == NULL) + if (parser == NULL) return (ENOMEM); + mt = calloc(1, sizeof *mt); + if (mt == NULL) { + XML_ParserFree(parser); + return (ENOMEM); + } mt->mesh = gmp; XML_SetUserData(parser, mt); XML_SetElementHandler(parser, StartElement, EndElement); XML_SetCharacterDataHandler(parser, CharData); i = XML_Parse(parser, p, strlen(p), 1); - if (i != 1) - return (-1); XML_ParserFree(parser); + if (i != 1) { + free(mt); + return (-1); + } gmp->lg_ident = calloc(sizeof *gmp->lg_ident, mt->nident + 1); + free(mt); if (gmp->lg_ident == NULL) return (ENOMEM); - free(mt); i = 0; /* Collect all identifiers */ LIST_FOREACH(cl, &gmp->lg_class, lg_class) {