Skip to content

Commit dd9cf23

Browse files
mhagstrandnikic
authored andcommitted
BUG #73998: Numeric properties are not accessible from get_object_vars
1 parent acda256 commit dd9cf23

File tree

4 files changed

+45
-2
lines changed

4 files changed

+45
-2
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ PHP NEWS
55
- Core:
66
. Fixed bug #73807 (Performance problem with processing large post request).
77
(Nikita)
8+
. Fixed bug #73998 (array_key_exists fails on arrays created by
9+
get_object_vars). (mhagstrand)
810

911
- GD:
1012
. Fixed bug #74031 (ReflectionFunction for imagepng is missing last two

Zend/tests/bug73998.phpt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
--TEST--
2+
Bug #73998 (array_key_exists fails on arrays created by get_object_vars)
3+
--DESCRIPTION--
4+
Properties of objects with numeric names should be accessible
5+
--FILE--
6+
<?php
7+
$a = new stdClass;
8+
$a->{1234} = "Numeric";
9+
$a->a1234 = "String";
10+
11+
$properties = get_object_vars($a);
12+
var_dump(array_key_exists(1234, $properties));
13+
echo "Value: {$properties[1234]}\n";
14+
15+
?>
16+
--EXPECT--
17+
bool(true)
18+
Value: Numeric
19+

Zend/zend_builtin_functions.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1146,6 +1146,8 @@ ZEND_FUNCTION(get_object_vars)
11461146
HashTable *properties;
11471147
zend_string *key;
11481148
zend_object *zobj;
1149+
zend_ulong index;
1150+
zend_bool fast_copy = 0;
11491151

11501152
ZEND_PARSE_PARAMETERS_START(1, 1)
11511153
Z_PARAM_OBJECT(obj)
@@ -1164,7 +1166,17 @@ ZEND_FUNCTION(get_object_vars)
11641166
zobj = Z_OBJ_P(obj);
11651167

11661168
if (!zobj->ce->default_properties_count && properties == zobj->properties && !ZEND_HASH_GET_APPLY_COUNT(properties)) {
1167-
/* fast copy */
1169+
fast_copy = 1;
1170+
/* Check if the object has a numeric property, See Bug 73998 */
1171+
ZEND_HASH_FOREACH_STR_KEY(properties, key) {
1172+
if (key && ZEND_HANDLE_NUMERIC(key, index)) {
1173+
fast_copy = 0;
1174+
break;
1175+
}
1176+
} ZEND_HASH_FOREACH_END();
1177+
}
1178+
1179+
if (fast_copy) {
11681180
if (EXPECTED(zobj->handlers == &std_object_handlers)) {
11691181
if (EXPECTED(!(GC_FLAGS(properties) & IS_ARRAY_IMMUTABLE))) {
11701182
GC_REFCOUNT(properties)++;
@@ -1190,7 +1202,7 @@ ZEND_FUNCTION(get_object_vars)
11901202
zend_unmangle_property_name_ex(key, &class_name, &prop_name, &prop_len);
11911203
zend_hash_str_add_new(Z_ARRVAL_P(return_value), prop_name, prop_len, value);
11921204
} else {
1193-
zend_hash_add_new(Z_ARRVAL_P(return_value), key, value);
1205+
zend_symbtable_add_new(Z_ARRVAL_P(return_value), key, value);
11941206
}
11951207
}
11961208
}

Zend/zend_hash.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,16 @@ static zend_always_inline int zend_hash_str_exists_ind(const HashTable *ht, cons
310310
Z_TYPE_P(Z_INDIRECT_P(zv)) != IS_UNDEF);
311311
}
312312

313+
static zend_always_inline zval *zend_symbtable_add_new(HashTable *ht, zend_string *key, zval *pData)
314+
{
315+
zend_ulong idx;
316+
317+
if (ZEND_HANDLE_NUMERIC(key, idx)) {
318+
return zend_hash_index_add_new(ht, idx, pData);
319+
} else {
320+
return zend_hash_add_new(ht, key, pData);
321+
}
322+
}
313323

314324
static zend_always_inline zval *zend_symtable_update(HashTable *ht, zend_string *key, zval *pData)
315325
{

0 commit comments

Comments
 (0)