[php-src] master: Merge branch 'PHP-8.6'
| From: | Ilia Alshanetsky | Date: | Tue, 29 Sep 2026 20:23:37 +0000 |
| Subject: | [php-src] master: Merge branch 'PHP-8.6' | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141740@lists.php.net to get a copy of this message | ||
Author: Ilia Alshanetsky (iliaal)
Date: 2026-09-29T16:21:07-04:00
Commit: https://github.com/php/php-src/commit/410e61d37d97f052c966a8711422b882fdfe3ef9
Raw diff: https://github.com/php/php-src/commit/410e61d37d97f052c966a8711422b882fdfe3ef9.diff
Merge branch 'PHP-8.6'
* PHP-8.6:
ext/tidy: Reject tidyNode use after the document is reparsed
Changed paths:
A ext/tidy/tests/reparse_node.phpt
M ext/tidy/tidy.c
Diff:
diff --git a/ext/tidy/tests/reparse_node.phpt b/ext/tidy/tests/reparse_node.phpt
new file mode 100644
index 000000000000..2ee03d8344e8
--- /dev/null
+++ b/ext/tidy/tests/reparse_node.phpt
@@ -0,0 +1,57 @@
+--TEST--
+tidyNode objects are invalid after reparsing their document
+--EXTENSIONS--
+tidy
+--FILE--
+<?php
+
+$tidy =
tidy_parse_string('<html><body><p>one</p><p>two</p></body></html>');
+$node = $tidy->body()->child[0];
+var_dump($node->isHtml());
+var_dump($node->hasSiblings());
+
+$tidy->parseString('<html><body><p>three</p></body></html>');
+
+$operations = [
+ 'string cast' => static fn() => (string) $node,
+ 'hasChildren' => static fn() => $node->hasChildren(),
+ 'hasSiblings' => static fn() => $node->hasSiblings(),
+ 'isComment' => static fn() => $node->isComment(),
+ 'isHtml' => static fn() => $node->isHtml(),
+ 'isText' => static fn() => $node->isText(),
+ 'isJste' => static fn() => $node->isJste(),
+ 'isAsp' => static fn() => $node->isAsp(),
+ 'isPhp' => static fn() => $node->isPhp(),
+ 'getParent' => static fn() => $node->getParent(),
+ 'getPreviousSibling' => static fn() => $node->getPreviousSibling(),
+ 'getNextSibling' => static fn() => $node->getNextSibling(),
+];
+
+foreach ($operations as $operation => $callback) {
+ try {
+ $callback();
+ echo $operation, ": no error\n";
+ } catch (Error $e) {
+ echo $operation, ': ', $e::class, ': ', $e->getMessage(),
"\n";
+ }
+}
+
+var_dump($tidy->body()->child[0]->isHtml());
+
+?>
+--EXPECT--
+bool(true)
+bool(true)
+string cast: Error: tidyNode object is no longer valid after its document was reparsed
+hasChildren: Error: tidyNode object is no longer valid after its document was reparsed
+hasSiblings: Error: tidyNode object is no longer valid after its document was reparsed
+isComment: Error: tidyNode object is no longer valid after its document was reparsed
+isHtml: Error: tidyNode object is no longer valid after its document was reparsed
+isText: Error: tidyNode object is no longer valid after its document was reparsed
+isJste: Error: tidyNode object is no longer valid after its document was reparsed
+isAsp: Error: tidyNode object is no longer valid after its document was reparsed
+isPhp: Error: tidyNode object is no longer valid after its document was reparsed
+getParent: Error: tidyNode object is no longer valid after its document was reparsed
+getPreviousSibling: Error: tidyNode object is no longer valid after its document was reparsed
+getNextSibling: Error: tidyNode object is no longer valid after its document was reparsed
+bool(true)
diff --git a/ext/tidy/tidy.c b/ext/tidy/tidy.c
index 98e1c05a720f..dcb87dd509b0 100644
--- a/ext/tidy/tidy.c
+++ b/ext/tidy/tidy.c
@@ -68,6 +68,11 @@
ZEND_PARSE_PARAMETERS_NONE(); \
obj = Z_TIDY_P(ZEND_THIS); \
+#define TIDY_FETCH_VALID_NODE \
+ TIDY_FETCH_ONLY_OBJECT; \
+ if (tidy_node_validate(obj) != SUCCESS) { \
+ RETURN_THROWS(); \
+ }
#define TIDY_SET_DEFAULT_CONFIG(_doc) \
if (TG(default_config) && TG(default_config)[0]) { \
php_tidy_load_config(_doc, TG(default_config)); \
@@ -94,12 +99,14 @@ struct _PHPTidyDoc {
TidyDoc doc;
TidyBuffer *errbuf;
uint32_t ref_count;
+ size_t parse_generation;
bool initialized;
};
struct _PHPTidyObj {
TidyNode node;
tidy_obj_type type;
+ size_t node_generation;
PHPTidyDoc *ptdoc;
zend_object std;
};
@@ -211,12 +218,28 @@ static zend_result php_tidy_apply_config(TidyDoc doc, const zend_string
*str_str
return SUCCESS;
}
+static zend_result tidy_node_validate(const PHPTidyObj *obj)
+{
+ if (!obj->ptdoc) {
+ zend_throw_error(NULL, "tidyNode object is not initialized");
+ return FAILURE;
+ }
+
+ if (obj->node_generation != obj->ptdoc->parse_generation) {
+ zend_throw_error(NULL, "tidyNode object is no longer valid after its document was
reparsed");
+ return FAILURE;
+ }
+
+ return SUCCESS;
+}
+
static void tidy_create_node_object(zval *zv, PHPTidyDoc *ptdoc, TidyNode node)
{
object_init_ex(zv, tidy_ce_node);
PHPTidyObj *newobj = Z_TIDY_P(zv);
newobj->node = node;
newobj->type = is_node;
+ newobj->node_generation = ptdoc->parse_generation;
newobj->ptdoc = ptdoc;
newobj->ptdoc->ref_count++;
tidy_add_node_default_properties(newobj);
@@ -374,6 +397,7 @@ static zend_object *tidy_object_new(zend_class_entry *class_type, const zend_obj
intern->ptdoc = emalloc(sizeof(PHPTidyDoc));
intern->ptdoc->doc = tidyCreate();
intern->ptdoc->ref_count = 1;
+ intern->ptdoc->parse_generation = 0;
intern->ptdoc->initialized = false;
intern->ptdoc->errbuf = emalloc(sizeof(TidyBuffer));
tidyBufInit(intern->ptdoc->errbuf);
@@ -468,6 +492,9 @@ static zend_result tidy_node_cast_handler(zend_object *in, zval *out, int type)
case IS_STRING:
obj = php_tidy_fetch_object(in);
+ if (tidy_node_validate(obj) != SUCCESS) {
+ return FAILURE;
+ }
tidyBufInit(&buf);
if (obj->ptdoc && tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf)) {
ZVAL_STRINGL(out, (const char *) buf.bp, buf.size-1);
@@ -524,6 +551,10 @@ static void tidy_add_node_default_properties(PHPTidyObj *obj)
zval attribute, children, temp;
const char *name;
+ if (tidy_node_validate(obj) != SUCCESS) {
+ return;
+ }
+
tidyBufInit(&buf);
(void) tidyNodeGetText(obj->ptdoc->doc, obj->node, &buf);
@@ -814,6 +845,7 @@ static zend_result php_tidy_parse_string(PHPTidyObj *obj, const zend_string *str
obj->ptdoc->initialized = true;
tidyBufInit(&buf);
+ obj->ptdoc->parse_generation++;
tidyBufAttach(&buf, (byte *) ZSTR_VAL(string), (unsigned int) ZSTR_LEN(string));
if (tidyParseBuffer(obj->ptdoc->doc, &buf) < 0) {
php_error_docref(NULL, E_WARNING, "%s", (const char*) obj->ptdoc->errbuf->bp);
@@ -1473,7 +1505,7 @@ PHP_FUNCTION(tidy_get_body)
/* {{{ Returns true if this node has children */
PHP_METHOD(tidyNode, hasChildren)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyGetChild(obj->node));
}
@@ -1482,7 +1514,7 @@ PHP_METHOD(tidyNode, hasChildren)
/* {{{ Returns true if this node has siblings */
PHP_METHOD(tidyNode, hasSiblings)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(obj->node && tidyGetNext(obj->node));
}
@@ -1491,7 +1523,7 @@ PHP_METHOD(tidyNode, hasSiblings)
/* {{{ Returns true if this node represents a comment */
PHP_METHOD(tidyNode, isComment)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Comment);
}
@@ -1500,7 +1532,7 @@ PHP_METHOD(tidyNode, isComment)
/* {{{ Returns true if this node is part of a HTML document */
PHP_METHOD(tidyNode, isHtml)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
switch (tidyNodeGetType(obj->node)) {
case TidyNode_Start:
@@ -1516,7 +1548,7 @@ PHP_METHOD(tidyNode, isHtml)
/* {{{ Returns true if this node represents text (no markup) */
PHP_METHOD(tidyNode, isText)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Text);
}
@@ -1525,7 +1557,7 @@ PHP_METHOD(tidyNode, isText)
/* {{{ Returns true if this node is JSTE */
PHP_METHOD(tidyNode, isJste)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Jste);
}
@@ -1534,7 +1566,7 @@ PHP_METHOD(tidyNode, isJste)
/* {{{ Returns true if this node is ASP */
PHP_METHOD(tidyNode, isAsp)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Asp);
}
@@ -1543,7 +1575,7 @@ PHP_METHOD(tidyNode, isAsp)
/* {{{ Returns true if this node is PHP */
PHP_METHOD(tidyNode, isPhp)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
RETURN_BOOL(tidyNodeGetType(obj->node) == TidyNode_Php);
}
@@ -1552,7 +1584,7 @@ PHP_METHOD(tidyNode, isPhp)
/* {{{ Returns the parent node if available or NULL */
PHP_METHOD(tidyNode, getParent)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
TidyNode parent_node = tidyGetParent(obj->node);
if (parent_node) {
@@ -1563,7 +1595,7 @@ PHP_METHOD(tidyNode, getParent)
PHP_METHOD(tidyNode, getPreviousSibling)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
TidyNode previous_node = tidyGetPrev(obj->node);
if (previous_node) {
@@ -1573,7 +1605,7 @@ PHP_METHOD(tidyNode, getPreviousSibling)
PHP_METHOD(tidyNode, getNextSibling)
{
- TIDY_FETCH_ONLY_OBJECT;
+ TIDY_FETCH_VALID_NODE;
TidyNode next_node = tidyGetNext(obj->node);
if (next_node) {