Skip to content

Commit

Permalink
Fix potential leak
Browse files Browse the repository at this point in the history
If the newVal is never set on a child's content entry, we will just end
up leaking that value at the end of the function. This should ensure that
if the value is used, even once, we do not free it at the end of the
function.

However, if it is not used at least once, we will free it at the end
of the function call.
  • Loading branch information
brianmichel committed Jan 12, 2015
1 parent ae277e1 commit 716343d
Showing 1 changed file with 11 additions and 0 deletions.
11 changes: 11 additions & 0 deletions HTMLNode.m
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ void setAttributeNamed(xmlNode * node, const char * nameStr, const char * value)
char * newVal = (char *)malloc(strlen(value)+1);
memcpy (newVal, value, strlen(value)+1);

bool copyUsed = false;

for(xmlAttrPtr attr = node->properties; NULL != attr; attr = attr->next)
{
if (strcmp((char*)attr->name, nameStr) == 0)
Expand All @@ -37,12 +39,21 @@ void setAttributeNamed(xmlNode * node, const char * nameStr, const char * value)
{
free(child->content);
child->content = (xmlChar*)newVal;

if (!copyUsed)
{
copyUsed = true;
}
break;
}
break;
}
}

if (!copyUsed)
{
free(newVal);
}

}

Expand Down

0 comments on commit 716343d

Please sign in to comment.