diff --git a/Zend/tests/036.phpt b/Zend/tests/036.phpt index 4037d3d0e3d21..8958237b0eb8b 100644 --- a/Zend/tests/036.phpt +++ b/Zend/tests/036.phpt @@ -11,4 +11,4 @@ try { ?> --EXPECT-- -Cannot access offset of type object on array +Cannot access offset of type Closure on array diff --git a/Zend/tests/038.phpt b/Zend/tests/038.phpt index 4f822a6f5a154..8b6441e3c25a3 100644 --- a/Zend/tests/038.phpt +++ b/Zend/tests/038.phpt @@ -11,4 +11,4 @@ try { ?> --EXPECT-- -Cannot access offset of type object on array +Cannot access offset of type Closure on array diff --git a/Zend/tests/assign_dim_obj_null_return.phpt b/Zend/tests/assign_dim_obj_null_return.phpt index 02e709818669e..e2b7f20a0c072 100644 --- a/Zend/tests/assign_dim_obj_null_return.phpt +++ b/Zend/tests/assign_dim_obj_null_return.phpt @@ -73,11 +73,11 @@ test(); --EXPECT-- Cannot add element to the array as the next element is already occupied Cannot access offset of type array on array -Cannot access offset of type object on array +Cannot access offset of type stdClass on array Cannot use a scalar value as an array Cannot add element to the array as the next element is already occupied Cannot access offset of type array on array -Cannot access offset of type object on array +Cannot access offset of type stdClass on array Cannot use a scalar value as an array Attempt to assign property "foo" on true Attempt to assign property "foo" on true diff --git a/Zend/tests/bug24773.phpt b/Zend/tests/bug24773.phpt index 4c73fd0dd00f2..f1845fa46a680 100644 --- a/Zend/tests/bug24773.phpt +++ b/Zend/tests/bug24773.phpt @@ -6,7 +6,7 @@ Bug #24773 (unset() of integers treated as arrays causes a crash) unset($array["lvl1"]["lvl2"]["b"]); ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Cannot access offset of type string on string in %s:%d +Fatal error: Uncaught Error: Cannot unset string offsets in %s:%d Stack trace: #0 {main} thrown in %s on line %d diff --git a/Zend/tests/gh8821.phpt b/Zend/tests/gh8821.phpt index e6abf5c1c4f1a..7588239fc83ba 100644 --- a/Zend/tests/gh8821.phpt +++ b/Zend/tests/gh8821.phpt @@ -15,7 +15,7 @@ new Bravo(); ?> --EXPECTF-- -Fatal error: Uncaught TypeError: Cannot access offset of type object on array in %sgh8821.php:8 +Fatal error: Uncaught TypeError: Cannot access offset of type Alpha on array in %sgh8821.php:8 Stack trace: #0 %sgh8821.php(11): [constant expression]() #1 {main} diff --git a/Zend/tests/illegal_offset_unset_isset_empty.phpt b/Zend/tests/illegal_offset_unset_isset_empty.phpt index a09613748281b..ee837f0b61439 100644 --- a/Zend/tests/illegal_offset_unset_isset_empty.phpt +++ b/Zend/tests/illegal_offset_unset_isset_empty.phpt @@ -22,6 +22,6 @@ try { ?> --EXPECT-- -Cannot access offset of type array in unset +Cannot unset offset of type array on array Cannot access offset of type array in isset or empty Cannot access offset of type array in isset or empty diff --git a/Zend/tests/init_array_illegal_offset_type.phpt b/Zend/tests/init_array_illegal_offset_type.phpt index 2e5a0401d6e4a..ee41c0217ad77 100644 --- a/Zend/tests/init_array_illegal_offset_type.phpt +++ b/Zend/tests/init_array_illegal_offset_type.phpt @@ -12,4 +12,4 @@ try { } ?> --EXPECT-- -Cannot access offset of type object on array +Cannot access offset of type stdClass on array diff --git a/Zend/tests/isset_array.phpt b/Zend/tests/isset_array.phpt index 792483294805d..dfa3fdef51dd7 100644 --- a/Zend/tests/isset_array.phpt +++ b/Zend/tests/isset_array.phpt @@ -47,4 +47,4 @@ bool(false) Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d bool(false) Cannot access offset of type array in isset or empty -Cannot access offset of type object in isset or empty +Cannot access offset of type stdClass in isset or empty diff --git a/Zend/tests/offset_array.phpt b/Zend/tests/offset_array.phpt index e44244511fcf1..368ec7a020e14 100644 --- a/Zend/tests/offset_array.phpt +++ b/Zend/tests/offset_array.phpt @@ -48,6 +48,6 @@ int(1) Warning: Resource ID#%d used as offset, casting to integer (%d) in %s on line %d int(%d) -Cannot access offset of type object on array +Cannot access offset of type stdClass on array Cannot access offset of type array on array Done diff --git a/Zend/zend.c b/Zend/zend.c index bbddd4597042b..0e3cfb4381fad 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1719,6 +1719,29 @@ ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const c } /* }}} */ +/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */ +ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type) +{ + switch (type) { + case BP_VAR_IS: + zend_type_error("Cannot access offset of type %s in isset or empty", + zend_zval_type_name(offset)); + return; + case BP_VAR_UNSET: + /* Consistent error for when trying to unset a string offset */ + if (zend_string_equals(container, ZSTR_KNOWN(ZEND_STR_STRING))) { + zend_throw_error(NULL, "Cannot unset string offsets"); + } else { + zend_type_error("Cannot unset offset of type %s on %s", zend_zval_type_name(offset), ZSTR_VAL(container)); + } + return; + default: + zend_type_error("Cannot access offset of type %s on %s", + zend_zval_type_name(offset), ZSTR_VAL(container)); + return; + } +} + ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) /* {{{ */ { va_list va; diff --git a/Zend/zend.h b/Zend/zend.h index fd21cbfeb93cf..94440530f3b36 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -357,6 +357,8 @@ ZEND_API ZEND_COLD void zend_throw_error(zend_class_entry *exception_ce, const c ZEND_API ZEND_COLD void zend_type_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2); ZEND_API ZEND_COLD void zend_argument_count_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2); ZEND_API ZEND_COLD void zend_value_error(const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 1, 2); +/* type should be one of the BP_VAR_* constants, only special messages happen for isset/empty and unset */ +ZEND_API ZEND_COLD void zend_illegal_container_offset(const zend_string *container, const zval *offset, int type); ZEND_COLD void zenderror(const char *error); diff --git a/Zend/zend_API.c b/Zend/zend_API.c index 61f15fec0c883..2bd236a26262e 100644 --- a/Zend/zend_API.c +++ b/Zend/zend_API.c @@ -407,16 +407,6 @@ ZEND_API ZEND_COLD void ZEND_FASTCALL zend_argument_error_variadic(zend_class_en } /* }}} */ -ZEND_API ZEND_COLD void zend_illegal_array_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset))); -} - -ZEND_API ZEND_COLD void zend_illegal_empty_or_isset_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset))); -} - ZEND_API ZEND_COLD void zend_argument_error(zend_class_entry *error_ce, uint32_t arg_num, const char *format, ...) /* {{{ */ { va_list va; @@ -2112,7 +2102,7 @@ ZEND_API zend_result array_set_zval_key(HashTable *ht, zval *key, zval *value) / result = zend_hash_index_update(ht, zend_dval_to_lval_safe(Z_DVAL_P(key)), value); break; default: - zend_illegal_array_offset(key); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), key, BP_VAR_W); result = NULL; } diff --git a/Zend/zend_execute.c b/Zend/zend_execute.c index caaedce98a850..29631504d5f90 100644 --- a/Zend/zend_execute.c +++ b/Zend/zend_execute.c @@ -1506,24 +1506,24 @@ static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_use_object_as_array(v zend_throw_error(NULL, "Cannot use object as array"); } -static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_unset_offset(const zval *offset) +static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_access(const zval *offset) { - zend_type_error("Cannot access offset of type %s in unset", zend_get_type_by_const(Z_TYPE_P(offset))); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_RW); } -static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset(const zval *offset) +static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_isset(const zval *offset) { - zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset))); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_IS); } -static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_empty_or_isset_offset(const zval *offset) +static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_array_offset_unset(const zval *offset) { - zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset))); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), offset, BP_VAR_UNSET); } -static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset) +static zend_never_inline ZEND_COLD void ZEND_FASTCALL zend_illegal_string_offset(const zval *offset, int type) { - zend_type_error("Cannot access offset of type %s on string", zend_zval_type_name(offset)); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), offset, type); } static zend_never_inline void zend_assign_to_object_dim(zend_object *obj, zval *dim, zval *value OPLINE_DC EXECUTE_DATA_DC) @@ -1651,7 +1651,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type } return offset; } - zend_illegal_string_offset(dim); + zend_illegal_string_offset(dim, type); return 0; } case IS_UNDEF: @@ -1667,7 +1667,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type dim = Z_REFVAL_P(dim); goto try_again; default: - zend_illegal_string_offset(dim); + zend_illegal_string_offset(dim, type); return 0; } @@ -2390,7 +2390,7 @@ static zend_never_inline uint8_t slow_index_convert(HashTable *ht, const zval *d value->lval = 1; return IS_LONG; default: - zend_illegal_array_offset(dim); + zend_illegal_array_offset_access(dim); return IS_NULL; } } @@ -2464,7 +2464,7 @@ static zend_never_inline uint8_t slow_index_convert_w(HashTable *ht, const zval value->lval = 1; return IS_LONG; default: - zend_illegal_array_offset(dim); + zend_illegal_array_offset_access(dim); return IS_NULL; } } @@ -2762,7 +2762,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z ZVAL_NULL(result); return; } - zend_illegal_string_offset(dim); + zend_illegal_string_offset(dim, BP_VAR_R); ZVAL_NULL(result); return; } @@ -2801,7 +2801,7 @@ static zend_always_inline void zend_fetch_dimension_address_read(zval *result, z dim = Z_REFVAL_P(dim); goto try_string_offset; default: - zend_illegal_string_offset(dim); + zend_illegal_string_offset(dim, BP_VAR_R); ZVAL_NULL(result); return; } @@ -2923,7 +2923,7 @@ static zend_never_inline zval* ZEND_FASTCALL zend_find_array_dim_slow(HashTable ZVAL_UNDEFINED_OP2(); goto str_idx; } else { - zend_illegal_empty_or_isset_offset(offset); + zend_illegal_array_offset_isset(offset); return NULL; } } @@ -3046,7 +3046,7 @@ static zend_never_inline bool ZEND_FASTCALL zend_array_key_exists_fast(HashTable str = ZSTR_EMPTY_ALLOC(); goto str_key; } else { - zend_illegal_array_offset(key); + zend_illegal_array_offset_access(key); return 0; } } diff --git a/Zend/zend_vm_def.h b/Zend/zend_vm_def.h index 7e86b29c6b4f7..437aa4b0c3b55 100644 --- a/Zend/zend_vm_def.h +++ b/Zend/zend_vm_def.h @@ -6098,7 +6098,7 @@ ZEND_VM_C_LABEL(num_index): str = ZSTR_EMPTY_ALLOC(); ZEND_VM_C_GOTO(str_index); } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } FREE_OP2(); @@ -6610,7 +6610,7 @@ ZEND_VM_C_LABEL(num_index_dim): key = ZSTR_EMPTY_ALLOC(); ZEND_VM_C_GOTO(str_index_dim); } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { diff --git a/Zend/zend_vm_execute.h b/Zend/zend_vm_execute.h index 14e3a5aca2a2a..8ec2f6946007e 100644 --- a/Zend/zend_vm_execute.h +++ b/Zend/zend_vm_execute.h @@ -7371,7 +7371,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CONST_C str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -9690,7 +9690,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CONST_T str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } zval_ptr_dtor_nogc(EX_VAR(opline->op2.var)); @@ -10613,7 +10613,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CONST_U str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -12063,7 +12063,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CONST_C str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -20063,7 +20063,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_TMP_CON str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -20507,7 +20507,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_TMP_TMP str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } zval_ptr_dtor_nogc(EX_VAR(opline->op2.var)); @@ -20968,7 +20968,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_TMP_UNU str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -21372,7 +21372,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_TMP_CV_ str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -25188,7 +25188,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_VAR_CON str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -25280,7 +25280,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_CONST_HANDL key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { @@ -27627,7 +27627,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_VAR_TMP str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } zval_ptr_dtor_nogc(EX_VAR(opline->op2.var)); @@ -27719,7 +27719,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_TMPVAR_HAND key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { @@ -29705,7 +29705,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_VAR_UNU str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -32015,7 +32015,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_VAR_CV_ str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -32107,7 +32107,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_VAR_CV_HANDLER( key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { @@ -43690,7 +43690,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CV_CONS str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -43782,7 +43782,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_CONST_HANDLE key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { @@ -47330,7 +47330,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CV_TMPV str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } zval_ptr_dtor_nogc(EX_VAR(opline->op2.var)); @@ -47422,7 +47422,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_TMPVAR_HANDL key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { @@ -49292,7 +49292,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CV_UNUS str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -52791,7 +52791,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_ADD_ARRAY_ELEMENT_SPEC_CV_CV_H str = ZSTR_EMPTY_ALLOC(); goto str_index; } else { - zend_illegal_array_offset(offset); + zend_illegal_array_offset_access(offset); zval_ptr_dtor_nogc(expr_ptr); } @@ -52883,7 +52883,7 @@ static ZEND_OPCODE_HANDLER_RET ZEND_FASTCALL ZEND_UNSET_DIM_SPEC_CV_CV_HANDLER(Z key = ZSTR_EMPTY_ALLOC(); goto str_index_dim; } else { - zend_illegal_unset_offset(offset); + zend_illegal_array_offset_unset(offset); } break; } else if (Z_ISREF_P(container)) { diff --git a/ext/opcache/jit/zend_jit_helpers.c b/ext/opcache/jit/zend_jit_helpers.c index 41c7e14a804cb..f9541bd7087dc 100644 --- a/ext/opcache/jit/zend_jit_helpers.c +++ b/ext/opcache/jit/zend_jit_helpers.c @@ -27,21 +27,6 @@ static ZEND_COLD void undef_result_after_exception(void) { } } -static ZEND_COLD void zend_jit_illegal_array_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s on array", zend_get_type_by_const(Z_TYPE_P(offset))); -} - -static ZEND_COLD void zend_jit_illegal_empty_or_isset_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset))); -} - -static ZEND_COLD void zend_jit_illegal_string_offset(zval *offset) -{ - zend_type_error("Cannot access offset of type %s on string", zend_zval_value_name(offset)); -} - static zend_never_inline zend_function* ZEND_FASTCALL _zend_jit_init_func_run_time_cache(zend_op_array *op_array) /* {{{ */ { void **run_time_cache; @@ -493,7 +478,7 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_r_helper(zend_array *ht, zval *dim, hval = 1; goto num_index; default: - zend_jit_illegal_array_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_R); undef_result_after_exception(); return; } @@ -635,7 +620,7 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_is_helper(zend_array *ht, zval *dim hval = 1; goto num_index; default: - zend_jit_illegal_array_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_IS); undef_result_after_exception(); return; } @@ -737,7 +722,7 @@ static int ZEND_FASTCALL zend_jit_fetch_dim_isset_helper(zend_array *ht, zval *d hval = 1; goto num_index; default: - zend_jit_illegal_empty_or_isset_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_IS); return 0; } @@ -873,7 +858,7 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_rw_helper(zend_array *ht, zval *di hval = 1; goto num_index; default: - zend_jit_illegal_array_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_RW); undef_result_after_exception(); return NULL; } @@ -1006,7 +991,7 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim hval = 1; goto num_index; default: - zend_jit_illegal_array_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_ARRAY), dim, BP_VAR_R); undef_result_after_exception(); if (EG(opline_before_exception) && (EG(opline_before_exception)+1)->opcode == ZEND_OP_DATA @@ -1029,7 +1014,8 @@ static zval* ZEND_FASTCALL zend_jit_fetch_dim_w_helper(zend_array *ht, zval *dim return retval; } -static zend_never_inline zend_long zend_check_string_offset(zval *dim/*, int type*/) +/* type is one of the BP_VAR_* constants */ +static zend_never_inline zend_long zend_check_string_offset(zval *dim, int type) { zend_long offset; @@ -1049,7 +1035,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim/*, int typ } return offset; } - zend_jit_illegal_string_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), dim, BP_VAR_R); return 0; } case IS_UNDEF: @@ -1065,7 +1051,7 @@ static zend_never_inline zend_long zend_check_string_offset(zval *dim/*, int typ dim = Z_REFVAL_P(dim); goto try_again; default: - zend_jit_illegal_string_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), dim, type); return 0; } @@ -1103,7 +1089,7 @@ static zend_string* ZEND_FASTCALL zend_jit_fetch_dim_str_r_helper(zend_string *s if (!(GC_FLAGS(str) & IS_STR_INTERNED)) { GC_ADDREF(str); } - offset = zend_check_string_offset(dim/*, BP_VAR_R*/); + offset = zend_check_string_offset(dim, BP_VAR_R); if (!(GC_FLAGS(str) & IS_STR_INTERNED) && UNEXPECTED(GC_DELREF(str) == 0)) { zend_string *ret = zend_jit_fetch_dim_str_offset(str, offset); zend_string_efree(str); @@ -1140,7 +1126,7 @@ static void ZEND_FASTCALL zend_jit_fetch_dim_str_is_helper(zend_string *str, zva dim = Z_REFVAL_P(dim); goto try_string_offset; default: - zend_jit_illegal_string_offset(dim); + zend_illegal_container_offset(ZSTR_KNOWN(ZEND_STR_STRING), dim, BP_VAR_IS); break; } @@ -1242,7 +1228,7 @@ static zend_never_inline void zend_assign_to_string_offset(zval *str, zval *dim, /* The string may be destroyed while throwing the notice. * Temporarily increase the refcount to detect this situation. */ GC_ADDREF(s); - offset = zend_check_string_offset(dim/*, BP_VAR_W*/); + offset = zend_check_string_offset(dim, BP_VAR_W); if (UNEXPECTED(GC_DELREF(s) == 0)) { zend_string_efree(s); if (result) { @@ -1418,7 +1404,7 @@ static zend_always_inline void ZEND_FASTCALL zend_jit_fetch_dim_obj_helper(zval zend_throw_error(NULL, "[] operator not supported for strings"); } else { if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) { - zend_check_string_offset(dim/*, BP_VAR_RW*/); + zend_check_string_offset(dim, BP_VAR_RW); } zend_wrong_string_offset_error(); } @@ -1606,7 +1592,7 @@ static void ZEND_FASTCALL zend_jit_assign_dim_op_helper(zval *container, zval *d zend_throw_error(NULL, "[] operator not supported for strings"); } else { if (UNEXPECTED(Z_TYPE_P(dim) != IS_LONG)) { - zend_check_string_offset(dim/*, BP_VAR_RW*/); + zend_check_string_offset(dim, BP_VAR_RW); } zend_wrong_string_offset_error(); } diff --git a/ext/opcache/tests/jit/assign_dim_002.phpt b/ext/opcache/tests/jit/assign_dim_002.phpt index 83b4bfdec7873..743ca1bf093ee 100644 --- a/ext/opcache/tests/jit/assign_dim_002.phpt +++ b/ext/opcache/tests/jit/assign_dim_002.phpt @@ -161,7 +161,7 @@ array(1) { int(1) } } -Cannot access offset of type object on array +Cannot access offset of type Closure on array array(1) { [0]=> array(2) { diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index 1ef0c48d272d6..0373f5a7820ea 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -95,21 +95,6 @@ static inline HashTable **spl_array_get_hash_table_ptr(spl_array_object* intern) } /* }}} */ -static void spl_array_illegal_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s on ArrayObject", zend_get_type_by_const(Z_TYPE_P(offset))); -} - -static void spl_array_illegal_empty_or_isset_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s in isset or empty", zend_get_type_by_const(Z_TYPE_P(offset))); -} - -static void spl_array_illegal_unset_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s in unset", zend_get_type_by_const(Z_TYPE_P(offset))); -} - static inline HashTable *spl_array_get_hash_table(spl_array_object* intern) { /* {{{ */ return *spl_array_get_hash_table_ptr(intern); } @@ -269,6 +254,8 @@ static void spl_hash_key_release(spl_hash_key *key) { } } +/* This function does not throw any exceptions for illegal offsets, calls to + * zend_illegal_container_offset(); need to be made if the return value is FAILURE */ static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zval *offset) { key->release_key = false; @@ -309,7 +296,6 @@ static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zva ZVAL_DEREF(offset); goto try_again; default: - spl_array_illegal_offset(offset); return FAILURE; } @@ -320,7 +306,8 @@ static zend_result get_hash_key(spl_hash_key *key, spl_array_object *intern, zva return SUCCESS; } -static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object *intern, zval *offset, int type) /* {{{ */ +static zval *spl_array_get_dimension_ptr(bool check_inherited, spl_array_object *intern, const zend_string *ce_name, + zval *offset, int type) /* {{{ */ { zval *retval; spl_hash_key key; @@ -336,7 +323,7 @@ static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object * } if (get_hash_key(&key, intern, offset) == FAILURE) { - spl_array_illegal_offset(offset); + zend_illegal_container_offset(ce_name, offset, type); return (type == BP_VAR_W || type == BP_VAR_RW) ? &EG(error_zval) : &EG(uninitialized_zval); } @@ -438,7 +425,7 @@ static zval *spl_array_read_dimension_ex(int check_inherited, zend_object *objec } } - ret = spl_array_get_dimension_ptr(check_inherited, intern, offset, type); + ret = spl_array_get_dimension_ptr(check_inherited, intern, object->ce->name, offset, type); /* When in a write context, * ZE has to be fooled into thinking this is in a reference set @@ -512,7 +499,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec } if (get_hash_key(&key, intern, offset) == FAILURE) { - spl_array_illegal_offset(offset); + zend_illegal_container_offset(object->ce->name, offset, BP_VAR_W); zval_ptr_dtor(value); return; } @@ -553,7 +540,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec } if (get_hash_key(&key, intern, offset) == FAILURE) { - spl_array_illegal_unset_offset(offset); + zend_illegal_container_offset(object->ce->name, offset, BP_VAR_UNSET); return; } @@ -623,7 +610,7 @@ static bool spl_array_has_dimension_ex(bool check_inherited, zend_object *object spl_hash_key key; if (get_hash_key(&key, intern, offset) == FAILURE) { - spl_array_illegal_empty_or_isset_offset(offset); + zend_illegal_container_offset(object->ce->name, offset, BP_VAR_IS); return 0; } @@ -861,7 +848,7 @@ static zval *spl_array_get_property_ptr_ptr(zend_object *object, zend_string *na return NULL; } ZVAL_STR(&member, name); - return spl_array_get_dimension_ptr(1, intern, &member, type); + return spl_array_get_dimension_ptr(1, intern, object->ce->name, &member, type); } return zend_std_get_property_ptr_ptr(object, name, type, cache_slot); } /* }}} */ diff --git a/ext/spl/spl_fixedarray.c b/ext/spl/spl_fixedarray.c index 574b06fc4e93c..bffdbbebcedca 100644 --- a/ext/spl/spl_fixedarray.c +++ b/ext/spl/spl_fixedarray.c @@ -83,11 +83,6 @@ static bool spl_fixedarray_empty(spl_fixedarray *array) return true; } -static void spl_fixedarray_illegal_offset(const zval *offset) -{ - zend_type_error("Cannot access offset of type %s on FixedArray", zend_get_type_by_const(Z_TYPE_P(offset))); -} - static void spl_fixedarray_default_ctor(spl_fixedarray *array) { array->size = 0; @@ -338,7 +333,8 @@ static zend_long spl_offset_convert_to_long(zval *offset) /* {{{ */ return Z_RES_HANDLE_P(offset); } - spl_fixedarray_illegal_offset(offset); + /* Use SplFixedArray name from the CE */ + zend_illegal_container_offset(spl_ce_SplFixedArray->name, offset, BP_VAR_R); return 0; } diff --git a/ext/spl/tests/ArrayObject_illegal_offset.phpt b/ext/spl/tests/ArrayObject_illegal_offset.phpt index 08353c704c6f3..a2803e4729663 100644 --- a/ext/spl/tests/ArrayObject_illegal_offset.phpt +++ b/ext/spl/tests/ArrayObject_illegal_offset.phpt @@ -36,4 +36,4 @@ Cannot access offset of type array on ArrayObject Cannot access offset of type array on ArrayObject Cannot access offset of type array on ArrayObject Cannot access offset of type array in isset or empty -Cannot access offset of type array in unset +Cannot unset offset of type array on ArrayObject diff --git a/ext/spl/tests/fixedarray_001.phpt b/ext/spl/tests/fixedarray_001.phpt index 35a7a9cf17725..0683555934d53 100644 --- a/ext/spl/tests/fixedarray_001.phpt +++ b/ext/spl/tests/fixedarray_001.phpt @@ -46,7 +46,7 @@ var_dump($b[0]); ?> --EXPECT-- RuntimeException: Index invalid or out of range -TypeError: Cannot access offset of type string on FixedArray +TypeError: Cannot access offset of type string on SplFixedArray RuntimeException: Index invalid or out of range string(6) "value0" string(6) "value2" diff --git a/ext/spl/tests/fixedarray_002.phpt b/ext/spl/tests/fixedarray_002.phpt index 940d5996f5dbc..0ee2dcb8ba11d 100644 --- a/ext/spl/tests/fixedarray_002.phpt +++ b/ext/spl/tests/fixedarray_002.phpt @@ -71,7 +71,7 @@ var_dump(count($a), $a->getSize(), count($a) == $a->getSize()); A::offsetSet RuntimeException: Index invalid or out of range A::offsetGet -TypeError: Cannot access offset of type string on FixedArray +TypeError: Cannot access offset of type string on SplFixedArray A::offsetUnset RuntimeException: Index invalid or out of range A::offsetSet diff --git a/ext/spl/tests/fixedarray_003.phpt b/ext/spl/tests/fixedarray_003.phpt index d246561c1b7e8..cca9ac07e9f7a 100644 --- a/ext/spl/tests/fixedarray_003.phpt +++ b/ext/spl/tests/fixedarray_003.phpt @@ -1,5 +1,5 @@ --TEST-- -SPL: FixedArray: Non integer offset handling +SPL: SplFixedArray: Non integer offset handling --FILE--