Skip to content

Commit 5200a7b

Browse files
committed
ext/spl: Fix no Error being thrown when appending ArrayObjey with object backing value
This aligns the behaviour of calling it with the append() method
1 parent 72ade29 commit 5200a7b

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

ext/spl/spl_array.c

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -466,6 +466,12 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
466466
HashTable *ht;
467467
spl_hash_key key;
468468

469+
/* Cannot append if backing value is an object */
470+
if (offset == NULL && spl_array_is_object(intern)) {
471+
zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(object->ce->name));
472+
return;
473+
}
474+
469475
if (check_inherited && intern->fptr_offset_set) {
470476
zval tmp;
471477

@@ -684,13 +690,6 @@ PHP_METHOD(ArrayObject, offsetSet)
684690

685691
void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
686692
{
687-
spl_array_object *intern = Z_SPLARRAY_P(object);
688-
689-
if (spl_array_is_object(intern)) {
690-
zend_throw_error(NULL, "Cannot append properties to objects, use %s::offsetSet() instead", ZSTR_VAL(Z_OBJCE_P(object)->name));
691-
return;
692-
}
693-
694693
spl_array_write_dimension(Z_OBJ_P(object), NULL, append_value);
695694
} /* }}} */
696695

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
--TEST--
2+
Cannot append to ArrayObject if backing value is an object
3+
--EXTENSIONS--
4+
spl
5+
--FILE--
6+
<?php
7+
class MyClass {}
8+
$c = new MyClass();
9+
10+
$ao = new ArrayObject($c);
11+
12+
try {
13+
$ao->append('no-key');
14+
var_dump($c);
15+
} catch (\Throwable $e) {
16+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
17+
}
18+
19+
try {
20+
$ao->append('no-key');
21+
var_dump($c);
22+
} catch (\Throwable $e) {
23+
echo $e::class, ': ', $e->getMessage(), PHP_EOL;
24+
}
25+
?>
26+
--EXPECT--
27+
Error: Cannot append properties to objects, use ArrayObject::offsetSet() instead
28+
Error: Cannot append properties to objects, use ArrayObject::offsetSet() instead

0 commit comments

Comments
 (0)