From f883aa6a4c8c66de129c9b20ee6ae22b68089565 Mon Sep 17 00:00:00 2001 From: Niels Dossche <7771979+nielsdos@users.noreply.github.com> Date: Tue, 27 Aug 2024 20:48:34 +0200 Subject: [PATCH] Avoid string duplication if possible in SimpleXMLElement::addAttribute() --- ext/simplexml/simplexml.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/ext/simplexml/simplexml.c b/ext/simplexml/simplexml.c index c98aa9381126..218fd69000b3 100644 --- a/ext/simplexml/simplexml.c +++ b/ext/simplexml/simplexml.c @@ -1753,7 +1753,8 @@ 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); @@ -1761,17 +1762,13 @@ PHP_METHOD(SimpleXMLElement, addAttribute) 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) { @@ -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); }