Skip to content

Resolve TODOs in ext/dom around nullable content #14999

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
Jul 17, 2024
Merged
Show file tree
Hide file tree
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
31 changes: 7 additions & 24 deletions ext/dom/characterdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,6 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-characterdata-substringdata
PHP_METHOD(DOMCharacterData, substringData)
{
zval *id;
xmlChar *cur;
xmlChar *substring;
xmlNodePtr node;
zend_long offset_input, count_input;
Expand All @@ -119,11 +118,7 @@ PHP_METHOD(DOMCharacterData, substringData)

DOM_GET_OBJ(node, id, xmlNodePtr, intern);

cur = node->content;
if (cur == NULL) {
/* TODO: is this even possible? */
cur = BAD_CAST "";
}
const xmlChar *cur = php_dom_get_content_or_empty(node);

length = xmlUTF8Strlen(cur);
if (ZEND_LONG_INT_OVFL(offset_input) || ZEND_LONG_INT_OVFL(count_input)) {
Expand Down Expand Up @@ -197,7 +192,7 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-characterdata-insertdata
static void dom_character_data_insert_data(INTERNAL_FUNCTION_PARAMETERS, bool return_true)
{
zval *id;
xmlChar *cur, *first, *second;
xmlChar *first, *second;
xmlNodePtr node;
char *arg;
zend_long offset_input;
Expand All @@ -213,11 +208,7 @@ static void dom_character_data_insert_data(INTERNAL_FUNCTION_PARAMETERS, bool re

DOM_GET_OBJ(node, id, xmlNodePtr, intern);

cur = node->content;
if (cur == NULL) {
/* TODO: is this even possible? */
cur = BAD_CAST "";
}
const xmlChar *cur = php_dom_get_content_or_empty(node);

length = xmlUTF8Strlen(cur);

Expand Down Expand Up @@ -268,7 +259,7 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-characterdata-deletedata
static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool return_true)
{
zval *id;
xmlChar *cur, *substring, *second;
xmlChar *substring, *second;
xmlNodePtr node;
zend_long offset, count_input;
unsigned int count;
Expand All @@ -282,11 +273,7 @@ static void dom_character_data_delete_data(INTERNAL_FUNCTION_PARAMETERS, bool re

DOM_GET_OBJ(node, id, xmlNodePtr, intern);

cur = node->content;
if (cur == NULL) {
/* TODO: is this even possible? */
cur = BAD_CAST "";
}
const xmlChar *cur = php_dom_get_content_or_empty(node);

length = xmlUTF8Strlen(cur);

Expand Down Expand Up @@ -340,7 +327,7 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-characterdata-replacedata
static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool return_true)
{
zval *id;
xmlChar *cur, *substring, *second = NULL;
xmlChar *substring, *second = NULL;
xmlNodePtr node;
char *arg;
zend_long offset, count_input;
Expand All @@ -356,11 +343,7 @@ static void dom_character_data_replace_data(INTERNAL_FUNCTION_PARAMETERS, bool r

DOM_GET_OBJ(node, id, xmlNodePtr, intern);

cur = node->content;
if (cur == NULL) {
/* TODO: is this even possible? */
cur = BAD_CAST "";
}
const xmlChar *cur = php_dom_get_content_or_empty(node);

length = xmlUTF8Strlen(cur);

Expand Down
5 changes: 5 additions & 0 deletions ext/dom/php_dom.h
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@ static zend_always_inline xmlNodePtr php_dom_first_child_of_container_node(xmlNo
}
}

static zend_always_inline const xmlChar *php_dom_get_content_or_empty(const xmlNode *node)
{
return node->content ? node->content : BAD_CAST "";
}

PHP_MINIT_FUNCTION(dom);
PHP_MSHUTDOWN_FUNCTION(dom);
PHP_MINFO_FUNCTION(dom);
Expand Down
33 changes: 33 additions & 0 deletions ext/dom/tests/null_text_content_manipulation.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--TEST--
NULL text content manipulation
--EXTENSIONS--
dom
--FILE--
<?php
$text = new DOMText();
try {
var_dump($text->substringData(1, 0));
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($text->insertData(1, ""));
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($text->deleteData(1, 1));
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
try {
var_dump($text->replaceData(1, 1, ""));
} catch (DOMException $e) {
echo $e->getMessage(), "\n";
}
?>
--EXPECT--
Index Size Error
Index Size Error
Index Size Error
Index Size Error
7 changes: 1 addition & 6 deletions ext/dom/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ Modern spec URL: https://dom.spec.whatwg.org/#dom-text-splittext
PHP_METHOD(DOMText, splitText)
{
zval *id;
xmlChar *cur;
xmlChar *first;
xmlChar *second;
xmlNodePtr node;
Expand All @@ -120,11 +119,7 @@ PHP_METHOD(DOMText, splitText)
RETURN_THROWS();
}

cur = node->content;
if (cur == NULL) {
/* TODO: is this even possible? */
cur = BAD_CAST "";
}
const xmlChar *cur = php_dom_get_content_or_empty(node);
length = xmlUTF8Strlen(cur);

if (ZEND_LONG_INT_OVFL(offset) || (int)offset > length) {
Expand Down
Loading