Skip to content

Avoid string duplication if possible in SimpleXMLElement::addAttribute() #15606

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 27, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions ext/simplexml/simplexml.c
Original file line number Diff line number Diff line change
Expand Up @@ -1753,25 +1753,22 @@ PHP_METHOD(SimpleXMLElement, addAttribute)
}

localname = xmlSplitQName2((xmlChar *)qname, &prefix);
if (localname == NULL) {
bool free_localname = localname != NULL;
if (!free_localname) {
if (nsuri_len > 0) {
if (prefix != NULL) {
xmlFree(prefix);
}
php_error_docref(NULL, E_WARNING, "Attribute requires prefix for namespace");
return;
}
localname = xmlStrdup((xmlChar *)qname);
localname = (xmlChar *) qname;
}

attrp = xmlHasNsProp(node, localname, (xmlChar *)nsuri);
if (attrp != NULL && attrp->type != XML_ATTRIBUTE_DECL) {
xmlFree(localname);
if (prefix != NULL) {
xmlFree(prefix);
}
php_error_docref(NULL, E_WARNING, "Attribute already exists");
return;
goto out;
}

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

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

xmlFree(localname);
out:
if (free_localname) {
xmlFree(localname);
}
if (prefix != NULL) {
xmlFree(prefix);
}
Expand Down
Loading