-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Fix crashes with entity references and predefined entities #13004
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--TEST-- | ||
Freeing of a predefined DOMEntityReference | ||
--EXTENSIONS-- | ||
dom | ||
--FILE-- | ||
<?php | ||
$ref = new DOMEntityReference("amp"); | ||
var_dump($ref); | ||
?> | ||
--EXPECT-- | ||
object(DOMEntityReference)#1 (17) { | ||
["nodeName"]=> | ||
string(3) "amp" | ||
["nodeValue"]=> | ||
NULL | ||
["nodeType"]=> | ||
int(5) | ||
["parentNode"]=> | ||
NULL | ||
["parentElement"]=> | ||
NULL | ||
["childNodes"]=> | ||
string(22) "(object value omitted)" | ||
["firstChild"]=> | ||
string(22) "(object value omitted)" | ||
["lastChild"]=> | ||
string(22) "(object value omitted)" | ||
["previousSibling"]=> | ||
NULL | ||
["nextSibling"]=> | ||
NULL | ||
["attributes"]=> | ||
NULL | ||
["isConnected"]=> | ||
bool(false) | ||
["namespaceURI"]=> | ||
NULL | ||
["prefix"]=> | ||
string(0) "" | ||
["localName"]=> | ||
NULL | ||
["baseURI"]=> | ||
NULL | ||
["textContent"]=> | ||
string(0) "" | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -206,12 +206,36 @@ static void php_libxml_node_free(xmlNodePtr node) | |||||
* dtd is attached to the document. This works around the issue by inspecting the parent directly. */ | ||||||
case XML_ENTITY_DECL: { | ||||||
xmlEntityPtr entity = (xmlEntityPtr) node; | ||||||
php_libxml_unlink_entity_decl(entity); | ||||||
if (entity->orig != NULL) { | ||||||
xmlFree((char *) entity->orig); | ||||||
entity->orig = NULL; | ||||||
if (entity->etype != XML_INTERNAL_PREDEFINED_ENTITY) { | ||||||
php_libxml_unlink_entity_decl(entity); | ||||||
#if LIBXML_VERSION >= 21200 | ||||||
xmlFreeEntity(entity); | ||||||
#else | ||||||
if (entity->children != NULL && entity->owner && entity == (xmlEntityPtr) entity->children->parent) { | ||||||
xmlFreeNodeList(entity->children); | ||||||
} | ||||||
xmlDictPtr dict = entity->doc != NULL ? entity->doc->dict : NULL; | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->name)) { | ||||||
xmlFree((xmlChar *) entity->name); | ||||||
} | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->ExternalID)) { | ||||||
xmlFree((xmlChar *) entity->ExternalID); | ||||||
} | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->SystemID)) { | ||||||
xmlFree((xmlChar *) entity->SystemID); | ||||||
} | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->URI)) { | ||||||
xmlFree((xmlChar *) entity->URI); | ||||||
} | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->content)) { | ||||||
xmlFree(entity->content); | ||||||
} | ||||||
if (dict == NULL || !xmlDictOwns(dict, entity->orig)) { | ||||||
xmlFree(entity->orig); | ||||||
} | ||||||
xmlFree(entity); | ||||||
#endif | ||||||
} | ||||||
xmlFreeNode(node); | ||||||
break; | ||||||
} | ||||||
case XML_NOTATION_NODE: { | ||||||
|
@@ -1385,6 +1409,15 @@ PHP_LIBXML_API void php_libxml_node_free_resource(xmlNodePtr node) | |||||
case XML_DOCUMENT_NODE: | ||||||
case XML_HTML_DOCUMENT_NODE: | ||||||
break; | ||||||
case XML_ENTITY_REF_NODE: | ||||||
/* Entity reference nodes are special: their children point to entity declarations, | ||||||
* but they don't own the declarations and therefore shouldn't free the children. | ||||||
* Moreover, there can be N>1 reference nodes for a single entity declarations. */ | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Fix:
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The suggestion looks good, thank you! |
||||||
php_libxml_unregister_node(node); | ||||||
if (node->parent == NULL) { | ||||||
php_libxml_node_free(node); | ||||||
} | ||||||
break; | ||||||
default: | ||||||
if (node->parent == NULL || node->type == XML_NAMESPACE_DECL) { | ||||||
php_libxml_node_free_list((xmlNodePtr) node->children); | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose it is unlikely that the extension is compiled against the newer version and an older libxml version is used at runtime.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should be not done, that's generally always a bad idea for libraries.