Skip to content

Commit b642138

Browse files
committed
Fix missing error for single index unset on wrong type
The error was correctly thrown for a nested index, but not for a single index. Make sure both have the same behavior.
1 parent 34257e1 commit b642138

File tree

3 files changed

+123
-0
lines changed

3 files changed

+123
-0
lines changed

Zend/tests/unset_non_array.phpt

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
--TEST--
2+
Unset on non-array
3+
--FILE--
4+
<?php
5+
6+
unset($x[0]);
7+
8+
$x = null;
9+
unset($x[0]);
10+
11+
$x = false;
12+
unset($x[0]);
13+
14+
$x = true;
15+
try {
16+
unset($x[0]);
17+
} catch (Error $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
21+
$x = 1;
22+
try {
23+
unset($x[0]);
24+
} catch (Error $e) {
25+
echo $e->getMessage(), "\n";
26+
}
27+
28+
$x = 3.14;
29+
try {
30+
unset($x[0]);
31+
} catch (Error $e) {
32+
echo $e->getMessage(), "\n";
33+
}
34+
35+
$x = "str";
36+
try {
37+
unset($x[0]);
38+
} catch (Error $e) {
39+
echo $e->getMessage(), "\n";
40+
}
41+
42+
$x = new stdClass;
43+
try {
44+
unset($x[0]);
45+
} catch (Error $e) {
46+
echo $e->getMessage(), "\n";
47+
}
48+
49+
// And now repeat the same with a nested offset.
50+
unset($x);
51+
52+
unset($x[0][0]);
53+
54+
$x = null;
55+
unset($x[0][0]);
56+
57+
$x = false;
58+
unset($x[0][0]);
59+
60+
$x = true;
61+
try {
62+
unset($x[0][0]);
63+
} catch (Error $e) {
64+
echo $e->getMessage(), "\n";
65+
}
66+
67+
$x = 1;
68+
try {
69+
unset($x[0][0]);
70+
} catch (Error $e) {
71+
echo $e->getMessage(), "\n";
72+
}
73+
74+
$x = 3.14;
75+
try {
76+
unset($x[0][0]);
77+
} catch (Error $e) {
78+
echo $e->getMessage(), "\n";
79+
}
80+
81+
$x = "str";
82+
try {
83+
unset($x[0][0]);
84+
} catch (Error $e) {
85+
echo $e->getMessage(), "\n";
86+
}
87+
88+
$x = new stdClass;
89+
try {
90+
unset($x[0][0]);
91+
} catch (Error $e) {
92+
echo $e->getMessage(), "\n";
93+
}
94+
95+
?>
96+
--EXPECTF--
97+
Warning: Undefined variable $x in %s on line %d
98+
Cannot unset offset in a non-array variable
99+
Cannot unset offset in a non-array variable
100+
Cannot unset offset in a non-array variable
101+
Cannot unset string offsets
102+
Cannot use object of type stdClass as array
103+
104+
Warning: Undefined variable $x in %s on line %d
105+
Cannot unset offset in a non-array variable
106+
Cannot unset offset in a non-array variable
107+
Cannot unset offset in a non-array variable
108+
Cannot unset string offsets
109+
Cannot use object of type stdClass as array

Zend/zend_vm_def.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6462,6 +6462,8 @@ ZEND_VM_C_LABEL(num_index_dim):
64626462
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
64636463
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
64646464
zend_throw_error(NULL, "Cannot unset string offsets");
6465+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
6466+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
64656467
}
64666468
} while (0);
64676469

Zend/zend_vm_execute.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24568,6 +24568,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_CONST_HANDL
2456824568
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
2456924569
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
2457024570
zend_throw_error(NULL, "Cannot unset string offsets");
24571+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
24572+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
2457124573
}
2457224574
} while (0);
2457324575

@@ -26743,6 +26745,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_TMPVAR_HAND
2674326745
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
2674426746
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
2674526747
zend_throw_error(NULL, "Cannot unset string offsets");
26748+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
26749+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
2674626750
}
2674726751
} while (0);
2674826752

@@ -30743,6 +30747,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_CV_HANDLER(
3074330747
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
3074430748
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
3074530749
zend_throw_error(NULL, "Cannot unset string offsets");
30750+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
30751+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
3074630752
}
3074730753
} while (0);
3074830754

@@ -41986,6 +41992,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_CONST_HANDLE
4198641992
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
4198741993
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
4198841994
zend_throw_error(NULL, "Cannot unset string offsets");
41995+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
41996+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
4198941997
}
4199041998
} while (0);
4199141999

@@ -45454,6 +45462,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_TMPVAR_HANDL
4545445462
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
4545545463
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
4545645464
zend_throw_error(NULL, "Cannot unset string offsets");
45465+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
45466+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
4545745467
}
4545845468
} while (0);
4545945469

@@ -50559,6 +50569,8 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_CV_HANDLER(Z
5055950569
Z_OBJ_HT_P(container)->unset_dimension(Z_OBJ_P(container), offset);
5056050570
} else if (UNEXPECTED(Z_TYPE_P(container) == IS_STRING)) {
5056150571
zend_throw_error(NULL, "Cannot unset string offsets");
50572+
} else if (UNEXPECTED(Z_TYPE_P(container) > IS_FALSE)) {
50573+
zend_throw_error(NULL, "Cannot unset offset in a non-array variable");
5056250574
}
5056350575
} while (0);
5056450576

0 commit comments

Comments
 (0)