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
- Cleaned up usage of `free` throughout the library (Issue #276)
- Fixed potential memory leak in `mxmlLoad*` functions (Issue #278, Issue #279)
- Fixed `mxmlSaveString` with a buffer size of 0 (Issue #284)
- Fixed `MXML_MINOR_VERSION` value in "mxml.h" (Issue #285)

View File

@ -3,7 +3,7 @@
*
* 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
* information.
@ -302,9 +302,7 @@ mxml_set_attr(mxml_node_t *node, /* I - Element node */
* Free the old value as needed...
*/
if (attr->value)
free(attr->value);
free(attr->value);
attr->value = value;
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
* 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
* if the node would produce an empty string or if the string cannot be
* allocated.
* using `free()` when you are done with it. `NULL` is returned if the node
* would produce an empty string or if the string cannot be allocated.
*
* The callback argument specifies a function that returns a whitespace
* string or NULL before and after each element. If @code MXML_NO_CALLBACK@
* is specified, whitespace will only be added before @code MXML_TEXT@ nodes
* string or `NULL` before and after each element. If `MXML_NO_CALLBACK`
* is specified, whitespace will only be added before `MXML_TEXT` nodes
* with leading whitespace and before attribute names inside opening
* element tags.
*/

View File

@ -3,7 +3,7 @@
*
* 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
* information.
@ -46,12 +46,8 @@ mxmlIndexDelete(mxml_index_t *ind) /* I - Index to delete */
* Free memory...
*/
if (ind->attr)
free(ind->attr);
if (ind->alloc_nodes)
free(ind->nodes);
free(ind->attr);
free(ind->nodes);
free(ind);
}

View File

@ -3,7 +3,7 @@
*
* 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
* information.
@ -772,17 +772,14 @@ mxml_free(mxml_node_t *node) /* I - Node */
switch (node->type)
{
case MXML_ELEMENT :
if (node->value.element.name)
free(node->value.element.name);
free(node->value.element.name);
if (node->value.element.num_attrs)
{
for (i = 0; i < node->value.element.num_attrs; i ++)
{
if (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].name);
free(node->value.element.attrs[i].value);
}
free(node->value.element.attrs);
@ -792,15 +789,13 @@ mxml_free(mxml_node_t *node) /* I - Node */
/* Nothing to do */
break;
case MXML_OPAQUE :
if (node->value.opaque)
free(node->value.opaque);
free(node->value.opaque);
break;
case MXML_REAL :
/* Nothing to do */
break;
case MXML_TEXT :
if (node->value.text.string)
free(node->value.text.string);
free(node->value.text.string);
break;
case MXML_CUSTOM :
if (node->value.custom.data &&

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...
*/
if (node->value.element.name)
free(node->value.element.name);
free(node->value.element.name);
node->value.element.name = strdup(name);
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...
*/
if (node->value.opaque)
free(node->value.opaque);
free(node->value.opaque);
node->value.opaque = strdup(opaque);
return (0);
@ -239,9 +235,7 @@ mxmlSetOpaquef(mxml_node_t *node, /* I - Node to set */
s = _mxml_vstrdupf(format, ap);
va_end(ap);
if (node->value.opaque)
free(node->value.opaque);
free(node->value.opaque);
node->value.opaque = s;
return (0);
@ -311,8 +305,7 @@ mxmlSetText(mxml_node_t *node, /* I - Node to set */
* 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.string = strdup(string);
@ -356,8 +349,7 @@ mxmlSetTextf(mxml_node_t *node, /* I - Node to set */
s = _mxml_vstrdupf(format, 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.string = s;