Skip to content

Commit 641fe23

Browse files
Improve illegal offset error messages (#10504)
Co-authored-by: Marcos Marcolin <marcos@ixcsoft.com.br>
1 parent 66a1a91 commit 641fe23

32 files changed

+151
-106
lines changed

Diff for: Zend/tests/036.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111

1212
?>
1313
--EXPECT--
14-
Illegal offset type
14+
Cannot access offset of type object on array

Diff for: Zend/tests/038.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111

1212
?>
1313
--EXPECT--
14-
Illegal offset type
14+
Cannot access offset of type object on array

Diff for: Zend/tests/assign_dim_obj_null_return.phpt

+4-4
Original file line numberDiff line numberDiff line change
@@ -72,12 +72,12 @@ test();
7272
?>
7373
--EXPECT--
7474
Cannot add element to the array as the next element is already occupied
75-
Illegal offset type
76-
Illegal offset type
75+
Cannot access offset of type array on array
76+
Cannot access offset of type object on array
7777
Cannot use a scalar value as an array
7878
Cannot add element to the array as the next element is already occupied
79-
Illegal offset type
80-
Illegal offset type
79+
Cannot access offset of type array on array
80+
Cannot access offset of type object on array
8181
Cannot use a scalar value as an array
8282
Attempt to assign property "foo" on true
8383
Attempt to assign property "foo" on true

Diff for: Zend/tests/bug79790.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ function b($a = array()[array ()]) {
88
}
99
?>
1010
--EXPECTF--
11-
Fatal error: Uncaught TypeError: Illegal offset type in %s:%d
11+
Fatal error: Uncaught TypeError: Cannot access offset of type array on array in %s:%d
1212
Stack trace:
1313
#0 %s(%d): b()
1414
#1 {main}

Diff for: Zend/tests/bug79947.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ try {
1212
var_dump($array);
1313
?>
1414
--EXPECT--
15-
Illegal offset type
15+
Cannot access offset of type array on array
1616
array(0) {
1717
}

Diff for: Zend/tests/bug80781.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ if (isset($array[$data]) or getPlugin($data)) {
2525

2626
?>
2727
--EXPECTF--
28-
Fatal error: Uncaught TypeError: Illegal offset type in isset or empty in %s:%d
28+
Fatal error: Uncaught TypeError: Cannot access offset of type array in isset or empty in %s:%d
2929
Stack trace:
3030
#0 {main}
3131
thrown in %s on line %d

Diff for: Zend/tests/constant_expressions_invalid_offset_type_error.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const C2 = [C1, [] => 1];
88

99
?>
1010
--EXPECTF--
11-
Fatal error: Uncaught TypeError: Illegal offset type in %s:%d
11+
Fatal error: Uncaught TypeError: Cannot access offset of type array on array in %s:%d
1212
Stack trace:
1313
#0 {main}
1414
thrown in %s on line %d

Diff for: Zend/tests/gh8821.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ new Bravo();
1515

1616
?>
1717
--EXPECTF--
18-
Fatal error: Uncaught TypeError: Illegal offset type in %sgh8821.php:8
18+
Fatal error: Uncaught TypeError: Cannot access offset of type object on array in %sgh8821.php:8
1919
Stack trace:
2020
#0 %sgh8821.php(11): [constant expression]()
2121
#1 {main}

Diff for: Zend/tests/illegal_offset_unset_isset_empty.phpt

+3-3
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,6 @@ try {
2222

2323
?>
2424
--EXPECT--
25-
Illegal offset type in unset
26-
Illegal offset type in isset or empty
27-
Illegal offset type in isset or empty
25+
Cannot access offset of type array in unset
26+
Cannot access offset of type array in isset or empty
27+
Cannot access offset of type array in isset or empty

Diff for: Zend/tests/init_array_illegal_offset_type.phpt

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ try {
1212
}
1313
?>
1414
--EXPECT--
15-
Illegal offset type
15+
Cannot access offset of type object on array

Diff for: Zend/tests/isset_array.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ bool(false)
4646

4747
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
4848
bool(false)
49-
Illegal offset type in isset or empty
50-
Illegal offset type in isset or empty
49+
Cannot access offset of type array in isset or empty
50+
Cannot access offset of type object in isset or empty

Diff for: Zend/tests/offset_array.phpt

+2-2
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ int(1)
4848

4949
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
5050
int(%d)
51-
Illegal offset type
52-
Illegal offset type
51+
Cannot access offset of type object on array
52+
Cannot access offset of type array on array
5353
Done

Diff for: Zend/zend_API.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -407,6 +407,16 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_en
407407
}
408408
/* }}} */
409409

410+
ZEND_API ZEND_COLD void zend_illegal_array_offset(const zval *offset)
411+
{
412+
zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset)));
413+
}
414+
415+
ZEND_API ZEND_COLD void zend_illegal_empty_or_isset_offset(const zval *offset)
416+
{
417+
zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset)));
418+
}
419+
410420
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
411421
{
412422
va_list va;
@@ -2074,7 +2084,7 @@ ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /
20742084
result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
20752085
break;
20762086
default:
2077-
zend_type_error("Illegal offset type");
2087+
zend_illegal_array_offset(key);
20782088
result = NULL;
20792089
}
20802090

Diff for: Zend/zend_execute.c

+16-6
Original file line numberDiff line numberDiff line change
@@ -1466,9 +1466,19 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(v
14661466
zend_throw_error(NULL, "Cannot use object as array");
14671467
}
14681468

1469-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_offset(void)
1469+
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_unset_offset(const zval *offset)
14701470
{
1471-
zend_type_error("Illegal offset type");
1471+
zend_type_error("Cannot access offset of type %s in unset", zend_get_type_by_const(Z_TYPE_P(offset)));
1472+
}
1473+
1474+
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset(const zval *offset)
1475+
{
1476+
zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset)));
1477+
}
1478+
1479+
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_empty_or_isset_offset(const zval *offset)
1480+
{
1481+
zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset)));
14721482
}
14731483

14741484
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset)
@@ -2340,7 +2350,7 @@ static zend_never_inline zend_uchar slow_index_convert(HashTable *ht, const zval
23402350
value->lval = 1;
23412351
return IS_LONG;
23422352
default:
2343-
zend_illegal_offset();
2353+
zend_illegal_array_offset(dim);
23442354
return IS_NULL;
23452355
}
23462356
}
@@ -2414,7 +2424,7 @@ static zend_never_inline zend_uchar slow_index_convert_w(HashTable *ht, const zv
24142424
value->lval = 1;
24152425
return IS_LONG;
24162426
default:
2417-
zend_illegal_offset();
2427+
zend_illegal_array_offset(dim);
24182428
return IS_NULL;
24192429
}
24202430
}
@@ -2873,7 +2883,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable
28732883
ZVAL_UNDEFINED_OP2();
28742884
goto str_idx;
28752885
} else {
2876-
zend_type_error("Illegal offset type in isset or empty");
2886+
zend_illegal_empty_or_isset_offset(offset);
28772887
return NULL;
28782888
}
28792889
}
@@ -2996,7 +3006,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable
29963006
str = ZSTR_EMPTY_ALLOC();
29973007
goto str_key;
29983008
} else {
2999-
zend_illegal_offset();
3009+
zend_illegal_array_offset(key);
30003010
return 0;
30013011
}
30023012
}

Diff for: Zend/zend_vm_def.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -6069,7 +6069,7 @@ ZEND_VM_C_LABEL(num_index):
60696069
str = ZSTR_EMPTY_ALLOC();
60706070
ZEND_VM_C_GOTO(str_index);
60716071
} else {
6072-
zend_illegal_offset();
6072+
zend_illegal_array_offset(offset);
60736073
zval_ptr_dtor_nogc(expr_ptr);
60746074
}
60756075
FREE_OP2();
@@ -6585,7 +6585,7 @@ ZEND_VM_C_LABEL(num_index_dim):
65856585
key = ZSTR_EMPTY_ALLOC();
65866586
ZEND_VM_C_GOTO(str_index_dim);
65876587
} else {
6588-
zend_type_error("Illegal offset type in unset");
6588+
zend_illegal_unset_offset(offset);
65896589
}
65906590
break;
65916591
} else if (Z_ISREF_P(container)) {

0 commit comments

Comments
 (0)