Skip to content

Commit e247461

Browse files
committed
Fix GH-17201: Dom\TokenList issues with interned string replace
If a bucket previously had a non-interned string, and is now replaced with an interned string, then the type flags still incorrectly state it's a non-interned string. This leads to the refcount being edited for interned strings, which in turn can lead to a crash when protect_memory is set. Closes GH-17207.
1 parent 634c147 commit e247461

File tree

3 files changed

+24
-1
lines changed

3 files changed

+24
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ PHP NEWS
2525

2626
- DOM:
2727
. Fixed bug GH-17145 (DOM memory leak). (nielsdos)
28+
. Fixed bug GH-17201 (Dom\TokenList issues with interned string replace).
29+
(nielsdos)
2830

2931
- FFI:
3032
. Fixed bug #79075 (FFI header parser chokes on comments). (nielsdos)

ext/dom/tests/gh17201.phpt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
GH-17201 (Dom\TokenList issues with interned string replace)
3+
--EXTENSIONS--
4+
dom
5+
--INI--
6+
opcache.protect_memory=1
7+
--FILE--
8+
<?php
9+
$dom = DOM\XMLDocument::createFromString('<root class="AA B C"/>');
10+
$element = $dom->documentElement;
11+
$list = $element->classList;
12+
$list->replace('AA', 'AB'); // Use interned string
13+
foreach ($list as $entry) {
14+
var_dump($entry);
15+
}
16+
?>
17+
--EXPECT--
18+
string(2) "AB"
19+
string(1) "B"
20+
string(1) "C"

ext/dom/token_list.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,8 @@ PHP_METHOD(Dom_TokenList, replace)
583583
/* It already exists, remove token instead. */
584584
zend_hash_del_bucket(token_set, bucket);
585585
} else {
586-
Z_STR(bucket->val) = new_token;
586+
/* Need to use ZVAL_STR instead of Z_STR to reset the type flags. */
587+
ZVAL_STR(&bucket->val, new_token);
587588
}
588589

589590
/* 5. Run the update steps. */

0 commit comments

Comments
 (0)