Skip to content

Commit f0d0e33

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 641fe23 commit f0d0e33

23 files changed

+102
-147
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/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
@@ -1728,6 +1728,24 @@ ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const c
17281728
}
17291729
/* }}} */
17301730

1731+
/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */
1732+
ZEND_API ZEND_COLD void zend_illegal_container_offset(const char *container, const zval *offset, int type)
1733+
{
1734+
switch (type) {
1735+
case BP_VAR_IS:
1736+
zend_type_error("Cannot access offset of type %s in isset or empty",
1737+
zend_zval_type_name(offset));
1738+
return;
1739+
case BP_VAR_UNSET:
1740+
zend_type_error("Cannot access offset of type %s in unset", zend_zval_type_name(offset));
1741+
return;
1742+
default:
1743+
zend_type_error("Cannot access offset of type %s on %s",
1744+
zend_zval_type_name(offset), container);
1745+
return;
1746+
}
1747+
}
1748+
17311749
ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */
17321750
{
17331751
va_list va;

Zend/zend.h

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

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

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;
@@ -2084,7 +2074,7 @@ ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) /
20842074
result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value);
20852075
break;
20862076
default:
2087-
zend_illegal_array_offset(key);
2077+
zend_illegal_container_offset("array", key, BP_VAR_W);
20882078
result = NULL;
20892079
}
20902080

Zend/zend_execute.c

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,26 +1466,6 @@ 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_unset_offset(const zval *offset)
1470-
{
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)));
1482-
}
1483-
1484-
static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset)
1485-
{
1486-
zend_type_error("Cannot access offset of type %s on string", zend_zval_type_name(offset));
1487-
}
1488-
14891469
static zend_never_inline void zend_assign_to_object_dim(zend_object *obj, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC)
14901470
{
14911471
obj->handlers->write_dimension(obj, dim, value);
@@ -1611,7 +1591,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
16111591
}
16121592
return offset;
16131593
}
1614-
zend_illegal_string_offset(dim);
1594+
zend_illegal_container_offset("string", dim, BP_VAR_R);
16151595
return 0;
16161596
}
16171597
case IS_UNDEF:
@@ -1627,7 +1607,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type
16271607
dim = Z_REFVAL_P(dim);
16281608
goto try_again;
16291609
default:
1630-
zend_illegal_string_offset(dim);
1610+
zend_illegal_container_offset("string", dim, BP_VAR_R);
16311611
return 0;
16321612
}
16331613

@@ -2350,7 +2330,7 @@ static zend_never_inline zend_uchar slow_index_convert(HashTable *ht, const zval
23502330
value->lval = 1;
23512331
return IS_LONG;
23522332
default:
2353-
zend_illegal_array_offset(dim);
2333+
zend_illegal_container_offset("array", dim, BP_VAR_R);
23542334
return IS_NULL;
23552335
}
23562336
}
@@ -2424,7 +2404,7 @@ static zend_never_inline zend_uchar slow_index_convert_w(HashTable *ht, const zv
24242404
value->lval = 1;
24252405
return IS_LONG;
24262406
default:
2427-
zend_illegal_array_offset(dim);
2407+
zend_illegal_container_offset("array", dim, BP_VAR_R);
24282408
return IS_NULL;
24292409
}
24302410
}
@@ -2722,7 +2702,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
27222702
ZVAL_NULL(result);
27232703
return;
27242704
}
2725-
zend_illegal_string_offset(dim);
2705+
zend_illegal_container_offset("string", dim, BP_VAR_R);
27262706
ZVAL_NULL(result);
27272707
return;
27282708
}
@@ -2761,7 +2741,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z
27612741
dim = Z_REFVAL_P(dim);
27622742
goto try_string_offset;
27632743
default:
2764-
zend_illegal_string_offset(dim);
2744+
zend_illegal_container_offset("string", dim, BP_VAR_R);
27652745
ZVAL_NULL(result);
27662746
return;
27672747
}
@@ -2883,7 +2863,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable
28832863
ZVAL_UNDEFINED_OP2();
28842864
goto str_idx;
28852865
} else {
2886-
zend_illegal_empty_or_isset_offset(offset);
2866+
zend_illegal_container_offset("array", offset, BP_VAR_IS);
28872867
return NULL;
28882868
}
28892869
}
@@ -3006,7 +2986,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable
30062986
str = ZSTR_EMPTY_ALLOC();
30072987
goto str_key;
30082988
} else {
3009-
zend_illegal_array_offset(key);
2989+
zend_illegal_container_offset("array", key, BP_VAR_R);
30102990
return 0;
30112991
}
30122992
}

Zend/zend_vm_def.h

Lines changed: 2 additions & 2 deletions
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_array_offset(offset);
6072+
zend_illegal_container_offset("array", offset, BP_VAR_R);
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_illegal_unset_offset(offset);
6588+
zend_illegal_container_offset("array", offset, BP_VAR_UNSET);
65896589
}
65906590
break;
65916591
} else if (Z_ISREF_P(container)) {

0 commit comments

Comments
 (0)