Skip to content

Commit cc64a8e

Browse files
committed
Fix Bug #81160: isset/empty doesn't throw a TypeError on invalid string offset
1 parent b4829dc commit cc64a8e

8 files changed

+240
-65
lines changed

Zend/tests/bug31098.phpt

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,26 @@ var_dump(isset($a->b));
99
$a = '0';
1010
var_dump(isset($a->b));
1111
$a = '';
12-
var_dump(isset($a['b']));
12+
13+
try {
14+
var_dump(isset($a['b']));
15+
} catch (\TypeError $e) {
16+
echo $e->getMessage(), "\n";
17+
}
1318
$a = 'a';
14-
var_dump(isset($a['b']));
19+
20+
try {
21+
var_dump(isset($a['b']));
22+
} catch (\TypeError $e) {
23+
echo $e->getMessage(), "\n";
24+
}
1525
$a = '0';
16-
var_dump(isset($a['b']));
26+
27+
try {
28+
var_dump(isset($a['b']));
29+
} catch (\TypeError $e) {
30+
echo $e->getMessage(), "\n";
31+
}
1732

1833
$simpleString = "Bogus String Text";
1934
echo isset($simpleString->wrong)?"bug\n":"ok\n";
@@ -46,11 +61,11 @@ echo $simpleString["0"] === "B"?"ok\n":"bug\n";
4661
bool(false)
4762
bool(false)
4863
bool(false)
49-
bool(false)
50-
bool(false)
51-
bool(false)
52-
ok
64+
Illegal offset type string for container of type string in isset or empty
65+
Illegal offset type string for container of type string in isset or empty
66+
Illegal offset type string for container of type string in isset or empty
5367
ok
68+
Illegal offset type string for container of type string in isset or empty
5469
ok
5570
ok
5671
ok

Zend/tests/bug60362.phpt

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ Bug #60362: non-existent sub-sub keys should not have values
44
<?php
55
$arr = array('exists' => 'foz');
66

7-
if (isset($arr['exists']['non_existent'])) {
8-
echo "sub-key 'non_existent' is set: ";
9-
var_dump($arr['exists']['non_existent']);
10-
} else {
11-
echo "sub-key 'non_existent' is not set.\n";
7+
try {
8+
if (isset($arr['exists']['non_existent'])) {
9+
echo "sub-key 'non_existent' is set: ";
10+
var_dump($arr['exists']['non_existent']);
11+
} else {
12+
echo "sub-key 'non_existent' is not set.\n";
13+
}
14+
} catch (\TypeError $e) {
15+
echo $e->getMessage(), \PHP_EOL;
1216
}
17+
1318
if (isset($arr['exists'][1])) {
1419
echo "sub-key 1 is set: ";
1520
var_dump($arr['exists'][1]);
@@ -32,11 +37,15 @@ if (isset($arr['exists'][1][0])) {
3237
}
3338

3439
echo "-------------------\n";
35-
if (empty($arr['exists']['non_existent'])) {
36-
echo "sub-key 'non_existent' is empty.\n";
37-
} else {
38-
echo "sub-key 'non_existent' is not empty: ";
39-
var_dump($arr['exists']['non_existent']);
40+
try {
41+
if (empty($arr['exists']['non_existent'])) {
42+
echo "sub-key 'non_existent' is empty.\n";
43+
} else {
44+
echo "sub-key 'non_existent' is not empty: ";
45+
var_dump($arr['exists']['non_existent']);
46+
}
47+
} catch (\TypeError $e) {
48+
echo $e->getMessage(), \PHP_EOL;
4049
}
4150
if (empty($arr['exists'][1])) {
4251
echo "sub-key 1 is empty.\n";
@@ -61,13 +70,13 @@ if (empty($arr['exists'][1][0])) {
6170
echo "DONE";
6271
?>
6372
--EXPECT--
64-
sub-key 'non_existent' is not set.
73+
Illegal offset type string for container of type string in isset or empty
6574
sub-key 1 is set: string(1) "o"
6675
-------------------
6776
sub-sub-key 'sub_sub' is not set.
6877
sub-sub-key 0 is set: string(1) "o"
6978
-------------------
70-
sub-key 'non_existent' is empty.
79+
Illegal offset type string for container of type string in isset or empty
7180
sub-key 1 is not empty: string(1) "o"
7281
-------------------
7382
sub-sub-key 'sub_sub' is empty.

Zend/tests/bug69889.phpt

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,18 @@ var_dump($foo[0] ?? "default");
99
var_dump($foo[5] ?? "default");
1010
var_dump(isset($foo[5]) ? $foo[5] : "default");
1111

12-
var_dump($foo["str"] ?? "default");
13-
var_dump(isset($foo["str"]) ? $foo["str"] : "default");
12+
var_dump($foo["str"] ?? "default"); // TODO Make this also throw a TypeError?
13+
14+
try {
15+
var_dump(isset($foo["str"]) ? $foo["str"] : "default");
16+
} catch (\TypeError $e) {
17+
echo $e->getMessage(), \PHP_EOL;
18+
}
1419

1520
?>
1621
--EXPECT--
1722
string(1) "t"
1823
string(7) "default"
1924
string(7) "default"
2025
string(7) "default"
21-
string(7) "default"
26+
Illegal offset type string for container of type string in isset or empty

Zend/tests/bug81160.phpt

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
--TEST--
2+
Bug #81160: isset/empty doesn't throw a TypeError on invalid string offset
3+
--FILE--
4+
<?php
5+
6+
$s = 'Hello';
7+
$o = new stdClass();
8+
$a = [];
9+
10+
try {
11+
var_dump(isset($s[$o]));
12+
} catch (\Throwable $e) {
13+
echo $e->getMessage(), "\n";
14+
}
15+
try {
16+
var_dump(empty($s[$o]));
17+
} catch (\Throwable $e) {
18+
echo $e->getMessage(), "\n";
19+
}
20+
try {
21+
var_dump(isset($s[$a]));
22+
} catch (\Throwable $e) {
23+
echo $e->getMessage(), "\n";
24+
}
25+
try {
26+
var_dump(empty($s[$a]));
27+
} catch (\Throwable $e) {
28+
echo $e->getMessage(), "\n";
29+
}
30+
try {
31+
var_dump(isset($s[[]]));
32+
} catch (\Throwable $e) {
33+
echo $e->getMessage(), "\n";
34+
}
35+
try {
36+
var_dump(empty($s[[]]));
37+
} catch (\Throwable $e) {
38+
echo $e->getMessage(), "\n";
39+
}
40+
41+
?>
42+
--EXPECT--
43+
Illegal offset type stdClass for container of type string in isset or empty
44+
Illegal offset type stdClass for container of type string in isset or empty
45+
Illegal offset type array for container of type string in isset or empty
46+
Illegal offset type array for container of type string in isset or empty
47+
Illegal offset type array for container of type string in isset or empty
48+
Illegal offset type array for container of type string in isset or empty

Zend/tests/empty_str_offset.phpt

Lines changed: 60 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,42 @@ var_dump(empty($str['-10']));
2323
var_dump(empty($str['0']));
2424
var_dump(empty($str['1']));
2525
var_dump(empty($str['4'])); // 0
26-
var_dump(empty($str['1.5']));
27-
var_dump(empty($str['good']));
28-
var_dump(empty($str['3 and a half']));
26+
try {
27+
var_dump(empty($str['1.5']));
28+
} catch (\TypeError $e) {
29+
echo $e->getMessage(), \PHP_EOL;
30+
}
31+
try {
32+
var_dump(empty($str['good']));
33+
} catch (\TypeError $e) {
34+
echo $e->getMessage(), \PHP_EOL;
35+
}
36+
try {
37+
var_dump(empty($str['3 and a half']));
38+
} catch (\TypeError $e) {
39+
echo $e->getMessage(), \PHP_EOL;
40+
}
2941
print "- string variable ---\n";
3042
var_dump(empty($str[$key = '-1'])); // 3
3143
var_dump(empty($str[$key = '-10']));
3244
var_dump(empty($str[$key = '0']));
3345
var_dump(empty($str[$key = '1']));
3446
var_dump(empty($str[$key = '4'])); // 0
35-
var_dump(empty($str[$key = '1.5']));
36-
var_dump(empty($str[$key = 'good']));
37-
var_dump(empty($str[$key = '3 and a half']));
47+
try {
48+
var_dump(empty($str[$key = '1.5']));
49+
} catch (\TypeError $e) {
50+
echo $e->getMessage(), \PHP_EOL;
51+
}
52+
try {
53+
var_dump(empty($str[$key = 'good']));
54+
} catch (\TypeError $e) {
55+
echo $e->getMessage(), \PHP_EOL;
56+
}
57+
try {
58+
var_dump(empty($str[$key = '3 and a half']));
59+
} catch (\TypeError $e) {
60+
echo $e->getMessage(), \PHP_EOL;
61+
}
3862
print "- bool ---\n";
3963
var_dump(empty($str[true]));
4064
var_dump(empty($str[false]));
@@ -52,13 +76,29 @@ var_dump(empty($str[0.9]));
5276
var_dump(empty($str[M_PI]));
5377
var_dump(empty($str[100.5001]));
5478
print "- array ---\n";
55-
var_dump(empty($str[array()]));
56-
var_dump(empty($str[array(1,2,3)]));
79+
try {
80+
var_dump(empty($str[array()]));
81+
} catch (\TypeError $e) {
82+
echo $e->getMessage(), \PHP_EOL;
83+
}
84+
try {
85+
var_dump(empty($str[array(1,2,3)]));
86+
} catch (\TypeError $e) {
87+
echo $e->getMessage(), \PHP_EOL;
88+
}
5789
print "- object ---\n";
58-
var_dump(empty($str[new stdClass()]));
90+
try {
91+
var_dump(empty($str[new stdClass()]));
92+
} catch (\TypeError $e) {
93+
echo $e->getMessage(), \PHP_EOL;
94+
}
5995
print "- resource ---\n";
6096
$f = fopen(__FILE__, 'r');
61-
var_dump(empty($str[$f]));
97+
try {
98+
var_dump(empty($str[$f]));
99+
} catch (\TypeError $e) {
100+
echo $e->getMessage(), \PHP_EOL;
101+
}
62102
print "done\n";
63103

64104
?>
@@ -79,18 +119,18 @@ bool(true)
79119
bool(false)
80120
bool(false)
81121
bool(true)
82-
bool(true)
83-
bool(true)
84-
bool(true)
122+
Illegal offset type string for container of type string in isset or empty
123+
Illegal offset type string for container of type string in isset or empty
124+
Illegal offset type string for container of type string in isset or empty
85125
- string variable ---
86126
bool(false)
87127
bool(true)
88128
bool(false)
89129
bool(false)
90130
bool(true)
91-
bool(true)
92-
bool(true)
93-
bool(true)
131+
Illegal offset type string for container of type string in isset or empty
132+
Illegal offset type string for container of type string in isset or empty
133+
Illegal offset type string for container of type string in isset or empty
94134
- bool ---
95135
bool(false)
96136
bool(false)
@@ -108,10 +148,10 @@ bool(false)
108148
bool(false)
109149
bool(true)
110150
- array ---
111-
bool(true)
112-
bool(true)
151+
Illegal offset type array for container of type string in isset or empty
152+
Illegal offset type array for container of type string in isset or empty
113153
- object ---
114-
bool(true)
154+
Illegal offset type stdClass for container of type string in isset or empty
115155
- resource ---
116-
bool(true)
156+
Illegal offset type resource for container of type string in isset or empty
117157
done

0 commit comments

Comments
 (0)