Skip to content

Commit

Permalink
Fix potential memory leak in mxmlLoadXxx (Issue #278, Issue #279)
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelrsweet committed Oct 26, 2021
1 parent 02f3310 commit bd4eb86
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
25 changes: 18 additions & 7 deletions mxml-file.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down Expand Up @@ -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;
}
}

/*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions testmxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
Expand All @@ -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);
}
Expand Down

0 comments on commit bd4eb86

Please sign in to comment.