Skip to content

Commit ba08538

Browse files
committed
Fix GH-18304: Changing the properties of a DateInterval through dynamic properties triggers a SegFault
For dynamic fetches the cache_slot will be NULL, so we have to check for that when resetting the cache. For zip and xmlreader this couldn't easily be tested because of a lack of writable properties. Closes GH-18307.
1 parent 29f96fb commit ba08538

File tree

14 files changed

+123
-11
lines changed

14 files changed

+123
-11
lines changed

NEWS

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 8.3.21
44

5+
- Core:
6+
. Fixed bug GH-18304 (Changing the properties of a DateInterval through
7+
dynamic properties triggers a SegFault). (nielsdos)
8+
59
- GD:
610
. Fixed imagecrop() overflow with rect argument with x/width y/heigh usage
711
in gdImageCrop(). (David Carlier)

ext/date/php_date.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -4399,7 +4399,9 @@ static zval *date_interval_get_property_ptr_ptr(zend_object *object, zend_string
43994399
zend_string_equals_literal(name, "days") ||
44004400
zend_string_equals_literal(name, "invert") ) {
44014401
/* Fallback to read_property. */
4402-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
4402+
if (cache_slot) {
4403+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
4404+
}
44034405
ret = NULL;
44044406
} else {
44054407
ret = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);

ext/date/tests/gh18304.phpt

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
--TEST--
2+
GH-18304 (Changing the properties of a DateInterval through dynamic properties triggers a SegFault)
3+
--CREDITS--
4+
orose-assetgo
5+
--FILE--
6+
<?php
7+
$di = new \DateInterval('P0Y');
8+
$field = 'd';
9+
$i = 1;
10+
$di->$field += $i;
11+
var_dump($di);
12+
?>
13+
--EXPECT--
14+
object(DateInterval)#1 (10) {
15+
["y"]=>
16+
int(0)
17+
["m"]=>
18+
int(0)
19+
["d"]=>
20+
int(1)
21+
["h"]=>
22+
int(0)
23+
["i"]=>
24+
int(0)
25+
["s"]=>
26+
int(0)
27+
["f"]=>
28+
float(0)
29+
["invert"]=>
30+
int(0)
31+
["days"]=>
32+
bool(false)
33+
["from_string"]=>
34+
bool(false)
35+
}

ext/dom/php_dom.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,9 @@ static zval *dom_get_property_ptr_ptr(zend_object *object, zend_string *name, in
303303
return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
304304
}
305305

306-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
306+
if (cache_slot) {
307+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
308+
}
307309
return NULL;
308310
}
309311

ext/dom/tests/gh18304.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-18304 (Changing the properties of a DateInterval through dynamic properties triggers a SegFault)
3+
--CREDITS--
4+
orose-assetgo
5+
--EXTENSIONS--
6+
dom
7+
--FILE--
8+
<?php
9+
$text = new \DOMText();
10+
$field = 'textContent';
11+
$text->$field .= 'hello';
12+
var_dump($text->$field);
13+
?>
14+
--EXPECT--
15+
string(5) "hello"

ext/pdo/pdo_stmt.c

+3-2
Original file line numberDiff line numberDiff line change
@@ -2493,9 +2493,10 @@ static zval *pdo_row_get_property_ptr_ptr(zend_object *object, zend_string *name
24932493
ZEND_IGNORE_VALUE(object);
24942494
ZEND_IGNORE_VALUE(name);
24952495
ZEND_IGNORE_VALUE(type);
2496-
ZEND_IGNORE_VALUE(cache_slot);
24972496

2498-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
2497+
if (cache_slot) {
2498+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
2499+
}
24992500
return NULL;
25002501
}
25012502

ext/simplexml/simplexml.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -635,7 +635,9 @@ static zval *sxe_property_get_adr(zend_object *object, zend_string *zname, int f
635635
SXE_ITER type;
636636
zval member;
637637

638-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
638+
if (cache_slot) {
639+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
640+
}
639641

640642
sxe = php_sxe_fetch_object(object);
641643
GET_NODE(sxe, node);

ext/simplexml/tests/gh18304.phpt

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
GH-18304 (Changing the properties of a DateInterval through dynamic properties triggers a SegFault)
3+
--CREDITS--
4+
orose-assetgo
5+
--EXTENSIONS--
6+
simplexml
7+
--FILE--
8+
<?php
9+
$sxe = simplexml_load_string('<root><abc/></root>');
10+
$field = 'abc';
11+
$sxe->$field .= 'hello';
12+
var_dump($sxe->$field);
13+
?>
14+
--EXPECT--
15+
object(SimpleXMLElement)#3 (1) {
16+
[0]=>
17+
string(5) "hello"
18+
}

ext/snmp/snmp.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -1861,7 +1861,9 @@ static zval *php_snmp_get_property_ptr_ptr(zend_object *object, zend_string *nam
18611861
return zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
18621862
}
18631863

1864-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
1864+
if (cache_slot) {
1865+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
1866+
}
18651867
return NULL;
18661868
}
18671869

ext/snmp/tests/gh18304.phpt

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--TEST--
2+
GH-18304 (Changing the properties of a DateInterval through dynamic properties triggers a SegFault)
3+
--CREDITS--
4+
orose-assetgo
5+
--EXTENSIONS--
6+
snmp
7+
--FILE--
8+
<?php
9+
$snmp = new SNMP(1, '127.0.0.1', 'community');
10+
$field = 'max_oids';
11+
$snmp->$field++;
12+
var_dump($snmp->$field);
13+
?>
14+
--EXPECT--
15+
int(1)

ext/spl/spl_array.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -846,7 +846,9 @@ static zval *spl_array_get_property_ptr_ptr(zend_object *object, zend_string *na
846846

847847
if ((intern->ar_flags & SPL_ARRAY_ARRAY_AS_PROPS) != 0
848848
&& !zend_std_has_property(object, name, ZEND_PROPERTY_EXISTS, NULL)) {
849-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
849+
if (cache_slot) {
850+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
851+
}
850852

851853
/* If object has offsetGet() overridden, then fallback to read_property,
852854
* which will call offsetGet(). */

ext/spl/tests/gh18304.phpt

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
--TEST--
2+
GH-18304 (Changing the properties of a DateInterval through dynamic properties triggers a SegFault)
3+
--CREDITS--
4+
orose-assetgo
5+
--FILE--
6+
<?php
7+
$ao = new ArrayObject(['abc' => 1]);
8+
$ao->setFlags(ArrayObject::ARRAY_AS_PROPS);
9+
$field = 'abc';
10+
$ao->$field++;
11+
var_dump($ao->$field);
12+
?>
13+
--EXPECT--
14+
int(2)

ext/xmlreader/php_xmlreader.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -121,8 +121,6 @@ zval *xmlreader_get_property_ptr_ptr(zend_object *object, zend_string *name, int
121121
zval *retval = NULL;
122122
xmlreader_prop_handler *hnd = NULL;
123123

124-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
125-
126124
obj = php_xmlreader_fetch_object(object);
127125

128126
if (obj->prop_handler != NULL) {
@@ -131,6 +129,8 @@ zval *xmlreader_get_property_ptr_ptr(zend_object *object, zend_string *name, int
131129

132130
if (hnd == NULL) {
133131
retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
132+
} else if (cache_slot) {
133+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
134134
}
135135

136136
return retval;

ext/zip/php_zip.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -890,8 +890,6 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name
890890
zval *retval = NULL;
891891
zip_prop_handler *hnd = NULL;
892892

893-
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
894-
895893
obj = php_zip_fetch_object(object);
896894

897895
if (obj->prop_handler != NULL) {
@@ -900,6 +898,8 @@ static zval *php_zip_get_property_ptr_ptr(zend_object *object, zend_string *name
900898

901899
if (hnd == NULL) {
902900
retval = zend_std_get_property_ptr_ptr(object, name, type, cache_slot);
901+
} else if (cache_slot) {
902+
cache_slot[0] = cache_slot[1] = cache_slot[2] = NULL;
903903
}
904904

905905
return retval;

0 commit comments

Comments
 (0)