Skip to content

Commit 9b73d59

Browse files
authored
Avoid string duplication if possible in SimpleXMLElement::addAttribute() (#15606)
1 parent 067eb8c commit 9b73d59

File tree

1 file changed

+8
-8
lines changed

1 file changed

+8
-8
lines changed

ext/simplexml/simplexml.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1753,25 +1753,22 @@ PHP_METHOD(SimpleXMLElement, addAttribute)
17531753
}
17541754

17551755
localname = xmlSplitQName2((xmlChar *)qname, &prefix);
1756-
if (localname == NULL) {
1756+
bool free_localname = localname != NULL;
1757+
if (!free_localname) {
17571758
if (nsuri_len > 0) {
17581759
if (prefix != NULL) {
17591760
xmlFree(prefix);
17601761
}
17611762
php_error_docref(NULL, E_WARNING, "Attribute requires prefix for namespace");
17621763
return;
17631764
}
1764-
localname = xmlStrdup((xmlChar *)qname);
1765+
localname = (xmlChar *) qname;
17651766
}
17661767

17671768
attrp = xmlHasNsProp(node, localname, (xmlChar *)nsuri);
17681769
if (attrp != NULL && attrp->type != XML_ATTRIBUTE_DECL) {
1769-
xmlFree(localname);
1770-
if (prefix != NULL) {
1771-
xmlFree(prefix);
1772-
}
17731770
php_error_docref(NULL, E_WARNING, "Attribute already exists");
1774-
return;
1771+
goto out;
17751772
}
17761773

17771774
if (nsuri != NULL) {
@@ -1783,7 +1780,10 @@ PHP_METHOD(SimpleXMLElement, addAttribute)
17831780

17841781
attrp = xmlNewNsProp(node, nsptr, localname, (xmlChar *)value);
17851782

1786-
xmlFree(localname);
1783+
out:
1784+
if (free_localname) {
1785+
xmlFree(localname);
1786+
}
17871787
if (prefix != NULL) {
17881788
xmlFree(prefix);
17891789
}

0 commit comments

Comments
 (0)