Skip to content

Commit 0c28b47

Browse files
committed
Fixed bug #79839
Add reference type sources in array_walk.
1 parent bc6979b commit 0c28b47

File tree

3 files changed

+37
-0
lines changed

3 files changed

+37
-0
lines changed

NEWS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ PHP NEWS
4949
. Fixed bug #70362 (Can't copy() large 'data://' with open_basedir). (cmb)
5050
. Fixed bug #78008 (dns_check_record() always return true on Alpine).
5151
(Andy Postnikov)
52+
. Fixed bug #79839 (array_walk() does not respect property types). (Nikita)
5253

5354
09 Jul 2020, PHP 7.4.8
5455

ext/standard/array.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1383,6 +1383,16 @@ static int php_array_walk(zval *array, zval *userdata, int recursive) /* {{{ */
13831383
zend_hash_move_forward_ex(target_hash, &pos);
13841384
continue;
13851385
}
1386+
1387+
/* Add type source for property references. */
1388+
if (Z_TYPE_P(zv) != IS_REFERENCE && Z_TYPE_P(array) == IS_OBJECT) {
1389+
zend_property_info *prop_info =
1390+
zend_get_typed_property_info_for_slot(Z_OBJ_P(array), zv);
1391+
if (prop_info) {
1392+
ZVAL_NEW_REF(zv, zv);
1393+
ZEND_REF_ADD_TYPE_SOURCE(Z_REF_P(zv), prop_info);
1394+
}
1395+
}
13861396
}
13871397

13881398
/* Ensure the value is a reference. Otherwise the location of the value may be freed. */
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
--TEST--
2+
Bug #79839: array_walk() does not respect property types
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public int $prop = 42;
8+
}
9+
10+
$test = new Test;
11+
try {
12+
array_walk($test, function(&$ref) {
13+
$ref = []; // Should throw
14+
});
15+
} catch (TypeError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
18+
var_dump($test);
19+
20+
?>
21+
--EXPECT--
22+
Cannot assign array to reference held by property Test::$prop of type int
23+
object(Test)#1 (1) {
24+
["prop"]=>
25+
&int(42)
26+
}

0 commit comments

Comments
 (0)