Skip to content

Commit e8e220e

Browse files
committed
Use common function for container offset type errors
This merges all usages of emiting an offset TypeError into a new ZEND_API function zend_illegal_container_offset(const char* container, const zval *offset, int type); Where the container should represent the type on which the access is attempted (e.g. string, array) The offset zval that is used, where the error message will display its type The type of access, which should be a BP_VAR_* constant, to get special message for isset/empty/unset
1 parent 6930ef5 commit e8e220e

24 files changed

+110
-153
lines changed

Zend/tests/036.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111

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

Zend/tests/038.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ try {
1111

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

Zend/tests/assign_dim_obj_null_return.phpt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,11 @@ test();
7373
--EXPECT--
7474
Cannot add element to the array as the next element is already occupied
7575
Cannot access offset of type array on array
76-
Cannot access offset of type object on array
76+
Cannot access offset of type stdClass 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
7979
Cannot access offset of type array on array
80-
Cannot access offset of type object on array
80+
Cannot access offset of type stdClass 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

Zend/tests/bug24773.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Bug #24773 (unset() of integers treated as arrays causes a crash)
66
unset($array["lvl1"]["lvl2"]["b"]);
77
?>
88
--EXPECTF--
9-
Fatal error: Uncaught TypeError: Cannot access offset of type string on string in %s:%d
9+
Fatal error: Uncaught TypeError: Cannot access offset of type string in unset in %s:%d
1010
Stack trace:
1111
#0 {main}
1212
thrown in %s on line %d

Zend/tests/gh8821.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ new Bravo();
1515

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

Zend/tests/init_array_illegal_offset_type.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ try {
1212
}
1313
?>
1414
--EXPECT--
15-
Cannot access offset of type object on array
15+
Cannot access offset of type stdClass on array

Zend/tests/isset_array.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ bool(false)
4747
Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d
4848
bool(false)
4949
Cannot access offset of type array in isset or empty
50-
Cannot access offset of type object in isset or empty
50+
Cannot access offset of type stdClass in isset or empty

Zend/tests/offset_array.phpt

Lines changed: 1 addition & 1 deletion
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-
Cannot access offset of type object on array
51+
Cannot access offset of type stdClass on array
5252
Cannot access offset of type array on array
5353
Done

Zend/zend.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1719,6 +1719,24 @@ ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const c
17191719
}
17201720
/* }}} */
17211721

1722+
/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */
1723+
ZEND_API ZEND_COLD void zend_illegal_container_offset(const char *container, const zval *offset, int type)
1724+
{
1725+
switch (type) {
1726+
case BP_VAR_IS:
1727+
zend_type_error("Cannot access offset of type %s in isset or empty",
1728+
zend_zval_type_name(offset));
1729+
return;
1730+
case BP_VAR_UNSET:
1731+
zend_type_error("Cannot access offset of type %s in unset", zend_zval_type_name(offset));
1732+
return;
1733+
default:
1734+
zend_type_error("Cannot access offset of type %s on %s",
1735+
zend_zval_type_name(offset), container);
1736+
return;
1737+
}
1738+
}
1739+
17221740
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
17231741
{
17241742
va_list va;

Zend/zend.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -357,6 +357,8 @@ ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const c
357357
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
358358
ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
359359
ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2);
360+
/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */
361+
ZEND_API ZEND_COLD void zend_illegal_container_offset(const char *container, const zval *offset, int type);
360362

361363
ZEND_COLD void zenderror(const char *error);
362364

Zend/zend_API.c

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -407,16 +407,6 @@ 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-
420410
ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */
421411
{
422412
va_list va;
@@ -2112,7 +2102,7 @@ ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /
21122102
result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
21132103
break;
21142104
default:
2115-
zend_illegal_array_offset(key);
2105+
zend_illegal_container_offset("array", key, BP_VAR_W);
21162106
result = NULL;
21172107
}
21182108

Zend/zend_execute.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1506,26 +1506,6 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(v
15061506
zend_throw_error(NULL, "Cannot use object as array");
15071507
}
15081508

1509-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_unset_offset(const zval *offset)
1510-
{
1511-
zend_type_error("Cannot access offset of type %s in unset", zend_get_type_by_const(Z_TYPE_P(offset)));
1512-
}
1513-
1514-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset(const zval *offset)
1515-
{
1516-
zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset)));
1517-
}
1518-
1519-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_empty_or_isset_offset(const zval *offset)
1520-
{
1521-
zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset)));
1522-
}
1523-
1524-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset)
1525-
{
1526-
zend_type_error("Cannot access offset of type %s on string", zend_zval_type_name(offset));
1527-
}
1528-
15291509
static zend_never_inline void zend_assign_to_object_dim(zend_object *obj, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
15301510
{
15311511
obj->handlers->write_dimension(obj, dim, value);
@@ -1651,7 +1631,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
16511631
}
16521632
return offset;
16531633
}
1654-
zend_illegal_string_offset(dim);
1634+
zend_illegal_container_offset("string", dim, type);
16551635
return 0;
16561636
}
16571637
case IS_UNDEF:
@@ -1667,7 +1647,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
16671647
dim = Z_REFVAL_P(dim);
16681648
goto try_again;
16691649
default:
1670-
zend_illegal_string_offset(dim);
1650+
zend_illegal_container_offset("string", dim, type);
16711651
return 0;
16721652
}
16731653

@@ -2390,7 +2370,7 @@ static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *d
23902370
value->lval = 1;
23912371
return IS_LONG;
23922372
default:
2393-
zend_illegal_array_offset(dim);
2373+
zend_illegal_container_offset("array", dim, BP_VAR_R);
23942374
return IS_NULL;
23952375
}
23962376
}
@@ -2464,7 +2444,7 @@ static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval
24642444
value->lval = 1;
24652445
return IS_LONG;
24662446
default:
2467-
zend_illegal_array_offset(dim);
2447+
zend_illegal_container_offset("array", dim, BP_VAR_W);
24682448
return IS_NULL;
24692449
}
24702450
}
@@ -2762,7 +2742,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
27622742
ZVAL_NULL(result);
27632743
return;
27642744
}
2765-
zend_illegal_string_offset(dim);
2745+
zend_illegal_container_offset("string", dim, BP_VAR_R);
27662746
ZVAL_NULL(result);
27672747
return;
27682748
}
@@ -2801,7 +2781,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
28012781
dim = Z_REFVAL_P(dim);
28022782
goto try_string_offset;
28032783
default:
2804-
zend_illegal_string_offset(dim);
2784+
zend_illegal_container_offset("string", dim, BP_VAR_R);
28052785
ZVAL_NULL(result);
28062786
return;
28072787
}
@@ -2923,7 +2903,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable
29232903
ZVAL_UNDEFINED_OP2();
29242904
goto str_idx;
29252905
} else {
2926-
zend_illegal_empty_or_isset_offset(offset);
2906+
zend_illegal_container_offset("array", offset, BP_VAR_IS);
29272907
return NULL;
29282908
}
29292909
}
@@ -3046,7 +3026,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable
30463026
str = ZSTR_EMPTY_ALLOC();
30473027
goto str_key;
30483028
} else {
3049-
zend_illegal_array_offset(key);
3029+
zend_illegal_container_offset("array", key, BP_VAR_R);
30503030
return 0;
30513031
}
30523032
}

Zend/zend_vm_def.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6098,7 +6098,7 @@ ZEND_VM_C_LABEL(num_index):
60986098
str = ZSTR_EMPTY_ALLOC();
60996099
ZEND_VM_C_GOTO(str_index);
61006100
} else {
6101-
zend_illegal_array_offset(offset);
6101+
zend_illegal_container_offset("array", offset, BP_VAR_W);
61026102
zval_ptr_dtor_nogc(expr_ptr);
61036103
}
61046104
FREE_OP2();
@@ -6610,7 +6610,7 @@ ZEND_VM_C_LABEL(num_index_dim):
66106610
key = ZSTR_EMPTY_ALLOC();
66116611
ZEND_VM_C_GOTO(str_index_dim);
66126612
} else {
6613-
zend_illegal_unset_offset(offset);
6613+
zend_illegal_container_offset("array", offset, BP_VAR_UNSET);
66146614
}
66156615
break;
66166616
} else if (Z_ISREF_P(container)) {

0 commit comments

Comments
 (0)