Skip to content

Commit 185fc35

Browse files
committed
Make ?? consistent with isset()
1 parent 21663e8 commit 185fc35

11 files changed

+82
-75
lines changed

Zend/tests/bug60362.phpt

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ if (isset($arr['exists'][1])) {
2323
}
2424

2525
echo "-------------------\n";
26-
if (isset($arr['exists']['non_existent']['sub_sub'])) {
27-
echo "sub-key 'sub_sub' is set: ";
28-
var_dump($arr['exists']['non_existent']['sub_sub']);
29-
} else {
30-
echo "sub-sub-key 'sub_sub' is not set.\n";
26+
try {
27+
if (isset($arr['exists']['non_existent']['sub_sub'])) {
28+
echo "sub-key 'sub_sub' is set: ";
29+
var_dump($arr['exists']['non_existent']['sub_sub']);
30+
} else {
31+
echo "sub-sub-key 'sub_sub' is not set.\n";
32+
}
33+
} catch (\TypeError $e) {
34+
echo $e->getMessage(), \PHP_EOL;
3135
}
3236
if (isset($arr['exists'][1][0])) {
3337
echo "sub-sub-key 0 is set: ";
@@ -55,11 +59,15 @@ if (empty($arr['exists'][1])) {
5559
}
5660

5761
echo "-------------------\n";
58-
if (empty($arr['exists']['non_existent']['sub_sub'])) {
59-
echo "sub-sub-key 'sub_sub' is empty.\n";
60-
} else {
61-
echo "sub-sub-key 'sub_sub' is not empty: ";
62-
var_dump($arr['exists']['non_existent']['sub_sub']);
62+
try {
63+
if (empty($arr['exists']['non_existent']['sub_sub'])) {
64+
echo "sub-sub-key 'sub_sub' is empty.\n";
65+
} else {
66+
echo "sub-sub-key 'sub_sub' is not empty: ";
67+
var_dump($arr['exists']['non_existent']['sub_sub']);
68+
}
69+
} catch (\TypeError $e) {
70+
echo $e->getMessage(), \PHP_EOL;
6371
}
6472
if (empty($arr['exists'][1][0])) {
6573
echo "sub-sub-key 0 is empty.\n";
@@ -73,12 +81,12 @@ echo "DONE";
7381
Cannot access offset of type string on string
7482
sub-key 1 is set: string(1) "o"
7583
-------------------
76-
sub-sub-key 'sub_sub' is not set.
84+
Cannot access offset of type string on string
7785
sub-sub-key 0 is set: string(1) "o"
7886
-------------------
7987
Cannot access offset of type string on string
8088
sub-key 1 is not empty: string(1) "o"
8189
-------------------
82-
sub-sub-key 'sub_sub' is empty.
90+
Cannot access offset of type string on string
8391
sub-sub-key 0 is not empty: string(1) "o"
8492
DONE

Zend/tests/bug62680.phpt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,17 @@ Bug #62680 (Function isset() throws fatal error on set array if non-existent key
33
--FILE--
44
<?php
55
$array = array("");
6-
var_dump(isset($array[0]["a"]["b"]));
7-
var_dump(isset($array[0]["a"]["b"]["c"]));
6+
try {
7+
var_dump(isset($array[0]["a"]["b"]));
8+
} catch (\TypeError $e) {
9+
echo $e->getMessage(), \PHP_EOL;
10+
}
11+
try {
12+
var_dump(isset($array[0]["a"]["b"]["c"]));
13+
} catch (\TypeError $e) {
14+
echo $e->getMessage(), \PHP_EOL;
15+
}
816
?>
917
--EXPECT--
10-
bool(false)
11-
bool(false)
18+
Cannot access offset of type string on string
19+
Cannot access offset of type string on string

Zend/tests/bug69889.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ 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"); // TODO Make this also throw a TypeError?
12+
try {
13+
var_dump($foo["str"] ?? "default");
14+
} catch (\TypeError $e) {
15+
echo $e->getMessage(), \PHP_EOL;
16+
}
1317

1418
try {
1519
var_dump(isset($foo["str"]) ? $foo["str"] : "default");
@@ -22,5 +26,5 @@ try {
2226
string(1) "t"
2327
string(7) "default"
2428
string(7) "default"
25-
string(7) "default"
29+
Cannot access offset of type string on string
2630
Cannot access offset of type string on string

Zend/tests/bug81160.phpt

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ try {
1717
} catch (\Throwable $e) {
1818
echo $e->getMessage(), "\n";
1919
}
20+
try {
21+
var_dump($s[$o] ?? 'default');
22+
} catch (\Throwable $e) {
23+
echo $e->getMessage(), "\n";
24+
}
2025
try {
2126
var_dump(isset($s[$a]));
2227
} catch (\Throwable $e) {
@@ -27,6 +32,11 @@ try {
2732
} catch (\Throwable $e) {
2833
echo $e->getMessage(), "\n";
2934
}
35+
try {
36+
var_dump($s[$a] ?? 'default');
37+
} catch (\Throwable $e) {
38+
echo $e->getMessage(), "\n";
39+
}
3040
try {
3141
var_dump(isset($s[[]]));
3242
} catch (\Throwable $e) {
@@ -37,13 +47,19 @@ try {
3747
} catch (\Throwable $e) {
3848
echo $e->getMessage(), "\n";
3949
}
40-
41-
// TODO handle ??
50+
try {
51+
var_dump($s[[]] ?? 'default');
52+
} catch (\Throwable $e) {
53+
echo $e->getMessage(), "\n";
54+
}
4255

4356
?>
4457
--EXPECT--
4558
Cannot access offset of type stdClass on string
4659
Cannot access offset of type stdClass on string
60+
Cannot access offset of type stdClass on string
61+
Cannot access offset of type array on string
62+
Cannot access offset of type array on string
4763
Cannot access offset of type array on string
4864
Cannot access offset of type array on string
4965
Cannot access offset of type array on string

Zend/tests/empty_str_offset.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ try {
6262
print "- bool ---\n";
6363
var_dump(empty($str[true]));
6464
var_dump(empty($str[false]));
65+
echo "Sub-keys:\n";
6566
var_dump(empty($str[false][true]));
6667
print "- null ---\n";
6768
var_dump(empty($str[null]));
@@ -142,6 +143,9 @@ bool(false)
142143

143144
Warning: String offset cast occurred in %s on line %d
144145
bool(false)
146+
Sub-keys:
147+
148+
Warning: String offset cast occurred in %s on line %d
145149

146150
Warning: String offset cast occurred in %s on line %d
147151
bool(true)

Zend/tests/float_to_int/warning_float_does_not_fit_zend_long_strings.phpt

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,12 +87,8 @@ Float variable
8787
Warning: String offset cast occurred in %s on line %d
8888
string(1) "H"
8989
Float casted to string compile
90-
91-
Warning: Uninitialized string offset 9223372036854775807 in %s on line %d
9290
TypeError
9391
Float string variable
94-
95-
Warning: Uninitialized string offset 9223372036854775807 in %s on line %d
9692
TypeError
9793
Attempt to assign
9894
Float

Zend/tests/isset_str_offset.phpt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ try {
6161
print "- bool ---\n";
6262
var_dump(isset($str[true]));
6363
var_dump(isset($str[false]));
64+
echo "Sub-keys:\n";
6465
var_dump(isset($str[false][true]));
6566
print "- null ---\n";
6667
var_dump(isset($str[null]));
@@ -139,6 +140,9 @@ bool(true)
139140

140141
Warning: String offset cast occurred in %s on line %d
141142
bool(true)
143+
Sub-keys:
144+
145+
Warning: String offset cast occurred in %s on line %d
142146

143147
Warning: String offset cast occurred in %s on line %d
144148
bool(false)

Zend/tests/offset_string.phpt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,8 +68,6 @@ string(1) "i"
6868
Warning: String offset cast occurred in %s on line %d
6969
string(1) "S"
7070
Cannot access offset of type resource on string
71-
72-
Warning: Object of class stdClass could not be converted to int in %s on line %d
7371
Cannot access offset of type stdClass on string
7472
Cannot access offset of type array on string
7573
Done

Zend/zend_execute.c

Lines changed: 6 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1460,7 +1460,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
14601460
goto try_again;
14611461
default:
14621462
zend_illegal_string_offset(dim);
1463-
break;
1463+
return 0;
14641464
}
14651465

14661466
offset = zval_get_long_func(dim, /* is_strict */ false);
@@ -2405,51 +2405,12 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
24052405
if (!is_list && EXPECTED(Z_TYPE_P(container) == IS_STRING)) {
24062406
zend_long offset;
24072407

2408-
try_string_offset:
2409-
if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) {
2410-
switch (Z_TYPE_P(dim)) {
2411-
case IS_STRING:
2412-
{
2413-
bool trailing_data = false;
2414-
/* For BC reasons we allow errors so that we can warn on leading numeric string */
2415-
if (IS_LONG == is_numeric_string_ex(Z_STRVAL_P(dim), Z_STRLEN_P(dim), &offset,
2416-
NULL, /* allow errors */ true, NULL, &trailing_data)) {
2417-
if (UNEXPECTED(trailing_data)) {
2418-
zend_error(E_WARNING, "Illegal string offset \"%s\"", Z_STRVAL_P(dim));
2419-
}
2420-
goto out;
2421-
}
2422-
if (type == BP_VAR_IS) {
2423-
ZVAL_NULL(result);
2424-
return;
2425-
}
2426-
zend_illegal_string_offset(dim);
2427-
break;
2428-
}
2429-
case IS_UNDEF:
2430-
ZVAL_UNDEFINED_OP2();
2431-
ZEND_FALLTHROUGH;
2432-
case IS_DOUBLE:
2433-
case IS_NULL:
2434-
case IS_FALSE:
2435-
case IS_TRUE:
2436-
if (type != BP_VAR_IS) {
2437-
zend_error(E_WARNING, "String offset cast occurred");
2438-
}
2439-
break;
2440-
case IS_REFERENCE:
2441-
dim = Z_REFVAL_P(dim);
2442-
goto try_string_offset;
2443-
default:
2444-
zend_illegal_string_offset(dim);
2445-
break;
2446-
}
2447-
2448-
offset = zval_get_long_func(dim, /* is_strict */ false);
2449-
} else {
2450-
offset = Z_LVAL_P(dim);
2408+
offset = zend_check_string_offset(dim, BP_VAR_IS EXECUTE_DATA_CC);
2409+
/* Illegal offset */
2410+
if (UNEXPECTED(EG(exception) != NULL)) {
2411+
ZVAL_NULL(result);
2412+
return;
24512413
}
2452-
out:
24532414

24542415
if (UNEXPECTED(Z_STRLEN_P(container) < ((offset < 0) ? -(size_t)offset : ((size_t)offset + 1)))) {
24552416
if (type != BP_VAR_IS) {

tests/strings/offsets_chaining_5.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ try {
1111
echo $e->getMessage(), \PHP_EOL;
1212
}
1313
var_dump($array['expected_array']['0foo']);
14-
var_dump(isset($array['expected_array']['foo']['bar']));
14+
try {
15+
var_dump(isset($array['expected_array']['foo']['bar']));
16+
} catch (\TypeError $e) {
17+
echo $e->getMessage(), \PHP_EOL;
18+
}
1519
var_dump($array['expected_array']['0foo']['0bar']);
1620
?>
1721
--EXPECTF--
@@ -21,7 +25,7 @@ Cannot access offset of type string on string
2125

2226
Warning: Illegal string offset "0foo" in %s on line %d
2327
string(1) "f"
24-
bool(false)
28+
Cannot access offset of type string on string
2529

2630
Warning: Illegal string offset "0foo" in %s on line %d
2731

tests/strings/offsets_general.phpt

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,11 @@ try {
1414
} catch (\TypeError $e) {
1515
echo $e->getMessage() . \PHP_EOL;
1616
}
17-
var_dump(isset($string["foo"]["bar"]));
17+
try {
18+
var_dump(isset($string["foo"]["bar"]));
19+
} catch (\TypeError $e) {
20+
echo $e->getMessage() . \PHP_EOL;
21+
}
1822

1923
?>
2024
--EXPECT--
@@ -24,4 +28,4 @@ string(1) "o"
2428
bool(true)
2529
bool(true)
2630
Cannot access offset of type string on string
27-
bool(false)
31+
Cannot access offset of type string on string

0 commit comments

Comments
 (0)