Skip to content

Commit 71fb835

Browse files
committed
Fix bug #80945: Don't throw undefined array key warning in ArrayObject unset()
This makes the behavior of ArrayObject the same as far plain arrays, which don't throw a warning when unsetting a key that already doesn't exit.
1 parent 805471e commit 71fb835

13 files changed

+9
-38
lines changed

NEWS

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@ PHP NEWS
1313
. Fixed bug #81135 (unknown help topic causes assertion failure). (krakjoe)
1414

1515
- SPL:
16-
. Fixed buf #81112 (Special json_encode behavior for SplFixedArray). (Nikita)
16+
. Fixed bug #81112 (Special json_encode behavior for SplFixedArray). (Nikita)
17+
. Fixed bug #80945 ("Notice: Undefined index" on unset() ArrayObject
18+
non-existing key). (Nikita)
1719

1820
10 Jun 2021, PHP 8.1.0alpha1
1921

Zend/tests/unset_cv10.phpt

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,7 @@ $a->offsetUnset('x');
1010
echo $x;
1111
echo "ok\n";
1212
?>
13-
--EXPECTF--
13+
--EXPECT--
1414
ok
15-
16-
Warning: Undefined array key "x" in %s on line %d
1715
ok
1816
ok

ext/spl/spl_array.c

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -554,9 +554,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
554554
if (data) {
555555
if (Z_TYPE_P(data) == IS_INDIRECT) {
556556
data = Z_INDIRECT_P(data);
557-
if (Z_TYPE_P(data) == IS_UNDEF) {
558-
zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
559-
} else {
557+
if (Z_TYPE_P(data) != IS_UNDEF) {
560558
zval_ptr_dtor(data);
561559
ZVAL_UNDEF(data);
562560
HT_FLAGS(ht) |= HASH_FLAG_HAS_EMPTY_IND;
@@ -565,17 +563,13 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
565563
spl_array_skip_protected(intern, ht);
566564
}
567565
}
568-
} else if (zend_hash_del(ht, key.key) == FAILURE) {
569-
zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
566+
} else {
567+
zend_hash_del(ht, key.key);
570568
}
571-
} else {
572-
zend_error(E_WARNING,"Undefined array key \"%s\"", ZSTR_VAL(key.key));
573569
}
574570
spl_hash_key_release(&key);
575571
} else {
576-
if (zend_hash_index_del(ht, key.h) == FAILURE) {
577-
zend_error(E_WARNING, "Undefined array key " ZEND_LONG_FMT, key.h);
578-
}
572+
zend_hash_index_del(ht, key.h);
579573
}
580574
} /* }}} */
581575

ext/spl/tests/arrayObject___construct_basic4.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ bool(true)
6363

6464
Warning: Undefined array key "prop" in %s on line %d
6565

66-
Warning: Undefined array key "prop" in %s on line %d
67-
6866
Warning: Undefined array key "prop" in %s on line %d
6967
NULL
7068
NULL

ext/spl/tests/arrayObject___construct_basic5.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ bool(true)
6363

6464
Warning: Undefined array key "prop" in %s on line %d
6565

66-
Warning: Undefined array key "prop" in %s on line %d
67-
6866
Warning: Undefined array key "prop" in %s on line %d
6967
NULL
7068
NULL

ext/spl/tests/arrayObject_magicMethods1.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ object(ArrayObject)#2 (1) {
170170
}
171171

172172
--> Unset existent, non-existent and dynamic:
173-
174-
Warning: Undefined array key "nonexistent" in %s on line %d
175173
Original wrapped object:
176174
object(UsesMagic)#1 (3) {
177175
["b"]=>

ext/spl/tests/arrayObject_magicMethods3.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,6 @@ object(ArrayObject)#2 (1) {
170170
}
171171

172172
--> Unset existent, non-existent and dynamic:
173-
174-
Warning: Undefined array key "nonexistent" in %s on line %d
175173
Original wrapped object:
176174
object(UsesMagic)#1 (3) {
177175
["b"]=>

ext/spl/tests/arrayObject_magicMethods4.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ object(UsesMagic)#2 (2) {
179179
}
180180

181181
--> Unset existent, non-existent and dynamic:
182-
183-
Warning: Undefined array key "nonexistent" in %s on line %d
184182
Original wrapped object:
185183
object(C)#1 (3) {
186184
["b"]=>

ext/spl/tests/arrayObject_magicMethods6.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,6 @@ object(UsesMagic)#2 (2) {
179179
}
180180

181181
--> Unset existent, non-existent and dynamic:
182-
183-
Warning: Undefined array key "nonexistent" in %s on line %d
184182
Original wrapped object:
185183
object(C)#1 (3) {
186184
["b"]=>

ext/spl/tests/array_001.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,10 +82,6 @@ NULL
8282

8383
Warning: Undefined array key "b" in %s on line %d
8484
NULL
85-
86-
Warning: Undefined array key 7 in %s on line %d
87-
88-
Warning: Undefined array key "c" in %s on line %d
8985
object(ArrayObject)#%d (1) {
9086
["storage":"ArrayObject":private]=>
9187
array(2) {

ext/spl/tests/array_010.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,6 @@ array(6) {
125125
[6]=>
126126
string(9) "changed 6"
127127
}
128-
129-
Warning: Undefined array key 7 in %s on line %d
130-
131-
Warning: Undefined array key "8th" in %s on line %d
132128
array(4) {
133129
[0]=>
134130
string(3) "1st"

ext/spl/tests/bug45622b.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,4 @@ Doesn't trigger __get.
2828
Warning: Undefined array key "prop1" in %s on line %d
2929
Doesn't trigger __set.
3030
Doesn't trigger __unset.
31-
32-
Warning: Undefined array key "prop3" in %s on line %d
3331
Shouldn't trigger __isset.

ext/spl/tests/bug66127.phpt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,5 @@ unset($items[0][0]);
2121
crash();
2222
echo "Worked!\n";
2323
?>
24-
--EXPECTF--
25-
Warning: Undefined array key 0 in %s on line %d
24+
--EXPECT--
2625
Worked!

0 commit comments

Comments
 (0)