Clean up usage of free (Issue #276)

This commit is contained in:
Michael R Sweet 2021-10-26 14:50:25 -04:00
parent 3a4ece2266
commit e058082021
No known key found for this signature in database
GPG Key ID: 999559A027815955
6 changed files with 21 additions and 40 deletions

View File

@ -1,5 +1,6 @@
# Changes in Mini-XML 3.2.1 # Changes in Mini-XML 3.2.1
- Cleaned up usage of `free` throughout the library (Issue #276)
- Fixed potential memory leak in `mxmlLoad*` functions (Issue #278, Issue #279) - Fixed potential memory leak in `mxmlLoad*` functions (Issue #278, Issue #279)
- Fixed `mxmlSaveString` with a buffer size of 0 (Issue #284) - Fixed `mxmlSaveString` with a buffer size of 0 (Issue #284)
- Fixed `MXML_MINOR_VERSION` value in "mxml.h" (Issue #285) - Fixed `MXML_MINOR_VERSION` value in "mxml.h" (Issue #285)

View File

@ -3,7 +3,7 @@
* *
* https://www.msweet.org/mxml * https://www.msweet.org/mxml
* *
* Copyright © 2003-2019 by Michael R Sweet. * Copyright © 2003-2021 by Michael R Sweet.
* *
* Licensed under Apache License v2.0. See the file "LICENSE" for more * Licensed under Apache License v2.0. See the file "LICENSE" for more
* information. * information.
@ -302,9 +302,7 @@ mxml_set_attr(mxml_node_t *node, /* I - Element node */
* Free the old value as needed... * Free the old value as needed...
*/ */
if (attr->value)
free(attr->value); free(attr->value);
attr->value = value; attr->value = value;
return (0); return (0);

View File

@ -187,13 +187,12 @@ mxmlLoadString(mxml_node_t *top, /* I - Top node */
* *
* This function returns a pointer to a string containing the textual * This function returns a pointer to a string containing the textual
* representation of the XML node tree. The string should be freed * representation of the XML node tree. The string should be freed
* using the free() function when you are done with it. @code NULL@ is returned * using `free()` when you are done with it. `NULL` is returned if the node
* if the node would produce an empty string or if the string cannot be * would produce an empty string or if the string cannot be allocated.
* allocated.
* *
* The callback argument specifies a function that returns a whitespace * The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If @code MXML_NO_CALLBACK@ * string or `NULL` before and after each element. If `MXML_NO_CALLBACK`
* is specified, whitespace will only be added before @code MXML_TEXT@ nodes * is specified, whitespace will only be added before `MXML_TEXT` nodes
* with leading whitespace and before attribute names inside opening * with leading whitespace and before attribute names inside opening
* element tags. * element tags.
*/ */

View File

@ -3,7 +3,7 @@
* *
* https://www.msweet.org/mxml * https://www.msweet.org/mxml
* *
* Copyright © 2003-2019 by Michael R Sweet. * Copyright © 2003-2021 by Michael R Sweet.
* *
* Licensed under Apache License v2.0. See the file "LICENSE" for more * Licensed under Apache License v2.0. See the file "LICENSE" for more
* information. * information.
@ -46,12 +46,8 @@ mxmlIndexDelete(mxml_index_t *ind) /* I - Index to delete */
* Free memory... * Free memory...
*/ */
if (ind->attr)
free(ind->attr); free(ind->attr);
if (ind->alloc_nodes)
free(ind->nodes); free(ind->nodes);
free(ind); free(ind);
} }

View File

@ -3,7 +3,7 @@
* *
* https://www.msweet.org/mxml * https://www.msweet.org/mxml
* *
* Copyright © 2003-2019 by Michael R Sweet. * Copyright © 2003-2021 by Michael R Sweet.
* *
* Licensed under Apache License v2.0. See the file "LICENSE" for more * Licensed under Apache License v2.0. See the file "LICENSE" for more
* information. * information.
@ -772,16 +772,13 @@ mxml_free(mxml_node_t *node) /* I - Node */
switch (node->type) switch (node->type)
{ {
case MXML_ELEMENT : case MXML_ELEMENT :
if (node->value.element.name)
free(node->value.element.name); free(node->value.element.name);
if (node->value.element.num_attrs) if (node->value.element.num_attrs)
{ {
for (i = 0; i < node->value.element.num_attrs; i ++) for (i = 0; i < node->value.element.num_attrs; i ++)
{ {
if (node->value.element.attrs[i].name)
free(node->value.element.attrs[i].name); free(node->value.element.attrs[i].name);
if (node->value.element.attrs[i].value)
free(node->value.element.attrs[i].value); free(node->value.element.attrs[i].value);
} }
@ -792,14 +789,12 @@ mxml_free(mxml_node_t *node) /* I - Node */
/* Nothing to do */ /* Nothing to do */
break; break;
case MXML_OPAQUE : case MXML_OPAQUE :
if (node->value.opaque)
free(node->value.opaque); free(node->value.opaque);
break; break;
case MXML_REAL : case MXML_REAL :
/* Nothing to do */ /* Nothing to do */
break; break;
case MXML_TEXT : case MXML_TEXT :
if (node->value.text.string)
free(node->value.text.string); free(node->value.text.string);
break; break;
case MXML_CUSTOM : case MXML_CUSTOM :

View File

@ -126,9 +126,7 @@ mxmlSetElement(mxml_node_t *node, /* I - Node to set */
* Free any old element value and set the new value... * Free any old element value and set the new value...
*/ */
if (node->value.element.name)
free(node->value.element.name); free(node->value.element.name);
node->value.element.name = strdup(name); node->value.element.name = strdup(name);
return (0); return (0);
@ -194,9 +192,7 @@ mxmlSetOpaque(mxml_node_t *node, /* I - Node to set */
* Free any old opaque value and set the new value... * Free any old opaque value and set the new value...
*/ */
if (node->value.opaque)
free(node->value.opaque); free(node->value.opaque);
node->value.opaque = strdup(opaque); node->value.opaque = strdup(opaque);
return (0); return (0);
@ -239,9 +235,7 @@ mxmlSetOpaquef(mxml_node_t *node, /* I - Node to set */
s = _mxml_vstrdupf(format, ap); s = _mxml_vstrdupf(format, ap);
va_end(ap); va_end(ap);
if (node->value.opaque)
free(node->value.opaque); free(node->value.opaque);
node->value.opaque = s; node->value.opaque = s;
return (0); return (0);
@ -311,7 +305,6 @@ mxmlSetText(mxml_node_t *node, /* I - Node to set */
* Free any old string value and set the new value... * Free any old string value and set the new value...
*/ */
if (node->value.text.string)
free(node->value.text.string); free(node->value.text.string);
node->value.text.whitespace = whitespace; node->value.text.whitespace = whitespace;
@ -356,7 +349,6 @@ mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
s = _mxml_vstrdupf(format, ap); s = _mxml_vstrdupf(format, ap);
va_end(ap); va_end(ap);
if (node->value.text.string)
free(node->value.text.string); free(node->value.text.string);
node->value.text.whitespace = whitespace; node->value.text.whitespace = whitespace;