diff --git a/CHANGES.md b/CHANGES.md index 8f7d154..b4863d2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,6 @@ # Changes in Mini-XML 3.2.1 +- Fixed potential memory leak in `mxmlLoad*` functions (Issue #278, Issue #279) - Fixed `MXML_MINOR_VERSION` value in "mxml.h" (Issue #285) - Fixed POSIX threading support for MingW (Issue #287) - Fixed some minor memory leaks found by Coverity. diff --git a/mxml-file.c b/mxml-file.c index d4ead1f..79353ed 100644 --- a/mxml-file.c +++ b/mxml-file.c @@ -1366,9 +1366,9 @@ mxml_load_data( mxml_sax_cb_t sax_cb, /* I - SAX callback or MXML_NO_CALLBACK */ void *sax_data) /* I - SAX user data */ { - mxml_node_t *node, /* Current node */ - *first, /* First node added */ - *parent; /* Current parent node */ + mxml_node_t *node = NULL, /* Current node */ + *first = NULL, /* First node added */ + *parent = NULL; /* Current parent node */ int line = 1, /* Current line number */ ch, /* Character from file */ whitespace; /* Non-zero if whitespace seen */ @@ -1933,8 +1933,13 @@ mxml_load_data( { (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data); - if (!mxmlRelease(node) && first == node) - first = NULL; + if (!mxmlRelease(node)) + { + if (first == node) + first = NULL; + + node = NULL; + } } /* @@ -1981,6 +1986,7 @@ mxml_load_data( { mxml_error("Expected > but got '%c' instead for element <%s/> on line %d.", ch, buffer, line); mxmlDelete(node); + node = NULL; goto error; } @@ -2013,8 +2019,13 @@ mxml_load_data( { (*sax_cb)(node, MXML_SAX_ELEMENT_CLOSE, sax_data); - if (!mxmlRelease(node) && first == node) - first = NULL; + if (!mxmlRelease(node)) + { + if (first == node) + first = NULL; + + node = NULL; + } } } diff --git a/testmxml.c b/testmxml.c index 3d2a9df..271c6f1 100644 --- a/testmxml.c +++ b/testmxml.c @@ -661,7 +661,7 @@ main(int argc, /* I - Number of command-line args */ memset(event_counts, 0, sizeof(event_counts)); if (argv[1][0] == '<') - mxmlSAXLoadString(NULL, argv[1], type_cb, sax_cb, NULL); + mxmlRelease(mxmlSAXLoadString(NULL, argv[1], type_cb, sax_cb, NULL)); else if ((fp = fopen(argv[1], "rb")) == NULL) { perror(argv[1]); @@ -673,7 +673,7 @@ main(int argc, /* I - Number of command-line args */ * Read the file... */ - mxmlSAXLoadFile(NULL, fp, type_cb, sax_cb, NULL); + mxmlRelease(mxmlSAXLoadFile(NULL, fp, type_cb, sax_cb, NULL)); fclose(fp); }