Skip to content

Commit c2a58ab

Browse files
committed
Throw DomException for DOM out-of-memory error conditions
A number of error conditions in DOM can only occur if libxml2 runs out of memory, at least as far as I can see. In such cases we currently do a silent "return false", which violates the DOM spec, and which code is very unlikely to handle sensibly. Switch these to throw a DomException with INVALID_STATE_ERR type. This error type is chosen because we use for similar checks elsewhere, for example: https://github.com/php/php-src/blob/a733b1ada7895f6fa5e349755a878cae9189e3f5/ext/dom/documentfragment.c#L45-L48 This changes some of the more obvious cases I spotted, but there are probably more. Closes GH-7049.
1 parent 28f6a2b commit c2a58ab

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

ext/dom/document.c

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,8 @@ PHP_METHOD(DOMDocument, createElement)
533533

534534
node = xmlNewDocNode(docp, NULL, (xmlChar *) name, (xmlChar *) value);
535535
if (!node) {
536-
RETURN_FALSE;
536+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
537+
RETURN_THROWS();
537538
}
538539

539540
DOM_RET_OBJ(node, &ret, intern);
@@ -558,9 +559,10 @@ PHP_METHOD(DOMDocument, createDocumentFragment)
558559

559560
DOM_GET_OBJ(docp, id, xmlDocPtr, intern);
560561

561-
node = xmlNewDocFragment(docp);
562+
node = xmlNewDocFragment(docp);
562563
if (!node) {
563-
RETURN_FALSE;
564+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
565+
RETURN_THROWS();
564566
}
565567

566568
DOM_RET_OBJ(node, &ret, intern);
@@ -589,7 +591,8 @@ PHP_METHOD(DOMDocument, createTextNode)
589591

590592
node = xmlNewDocText(docp, (xmlChar *) value);
591593
if (!node) {
592-
RETURN_FALSE;
594+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
595+
RETURN_THROWS();
593596
}
594597

595598
DOM_RET_OBJ(node, &ret, intern);
@@ -618,7 +621,8 @@ PHP_METHOD(DOMDocument, createComment)
618621

619622
node = xmlNewDocComment(docp, (xmlChar *) value);
620623
if (!node) {
621-
RETURN_FALSE;
624+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
625+
RETURN_THROWS();
622626
}
623627

624628
DOM_RET_OBJ(node, &ret, intern);
@@ -647,7 +651,8 @@ PHP_METHOD(DOMDocument, createCDATASection)
647651

648652
node = xmlNewCDataBlock(docp, (xmlChar *) value, value_len);
649653
if (!node) {
650-
RETURN_FALSE;
654+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
655+
RETURN_THROWS();
651656
}
652657

653658
DOM_RET_OBJ(node, &ret, intern);
@@ -681,7 +686,8 @@ PHP_METHOD(DOMDocument, createProcessingInstruction)
681686

682687
node = xmlNewPI((xmlChar *) name, (xmlChar *) value);
683688
if (!node) {
684-
RETURN_FALSE;
689+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
690+
RETURN_THROWS();
685691
}
686692

687693
node->doc = docp;
@@ -717,7 +723,8 @@ PHP_METHOD(DOMDocument, createAttribute)
717723

718724
node = xmlNewDocProp(docp, (xmlChar *) name, NULL);
719725
if (!node) {
720-
RETURN_FALSE;
726+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
727+
RETURN_THROWS();
721728
}
722729

723730
DOM_RET_OBJ((xmlNodePtr) node, &ret, intern);
@@ -752,7 +759,8 @@ PHP_METHOD(DOMDocument, createEntityReference)
752759

753760
node = xmlNewReference(docp, (xmlChar *) name);
754761
if (!node) {
755-
RETURN_FALSE;
762+
php_dom_throw_error(INVALID_STATE_ERR, /* strict */ true);
763+
RETURN_THROWS();
756764
}
757765

758766
DOM_RET_OBJ((xmlNodePtr) node, &ret, intern);

ext/dom/php_dom.stub.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -467,10 +467,10 @@ public function createAttributeNS(?string $namespace, string $qualifiedName) {}
467467
/** @return DOMCdataSection|false */
468468
public function createCDATASection(string $data) {}
469469

470-
/** @return DOMComment|false */
470+
/** @return DOMComment */
471471
public function createComment(string $data) {}
472472

473-
/** @return DOMDocumentFragment|false */
473+
/** @return DOMDocumentFragment */
474474
public function createDocumentFragment() {}
475475

476476
/** @return DOMElement|false */
@@ -485,7 +485,7 @@ public function createEntityReference(string $name) {}
485485
/** @return DOMProcessingInstruction|false */
486486
public function createProcessingInstruction(string $target, string $data = "") {}
487487

488-
/** @return DOMText|false */
488+
/** @return DOMText */
489489
public function createTextNode(string $data) {}
490490

491491
/** @return DOMElement|null */

ext/dom/php_dom_arginfo.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/* This is a generated file, edit the .stub.php file instead.
2-
* Stub hash: d67979083e0adf438b47b3c7296341a4b860fc88 */
2+
* Stub hash: aa455c75920e15b5f66964cb560e853d6bc423d2 */
33

44
ZEND_BEGIN_ARG_WITH_RETURN_OBJ_INFO_EX(arginfo_dom_import_simplexml, 0, 1, DOMElement, 1)
55
ZEND_ARG_TYPE_INFO(0, node, IS_OBJECT, 0)

0 commit comments

Comments
 (0)