Skip to content

Convert SPL Exception usage to normal Error #4992

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 13 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 38 additions & 12 deletions ext/spl/php_spl.c
Original file line number Diff line number Diff line change
Expand Up @@ -523,50 +523,76 @@ PHP_FUNCTION(spl_autoload_register)
if (Z_TYPE_P(zcallable) == IS_ARRAY) {
if (!obj_ptr && alfi.func_ptr && !(alfi.func_ptr->common.fn_flags & ZEND_ACC_STATIC)) {
if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array specifies a non static method but no object (%s)", error);
zend_type_error("Passed array specifies a non static method but no object (%s)", error);
}
if (error) {
efree(error);
}
zend_string_release_ex(func_name, 0);
RETURN_FALSE;

if (do_throw) {
RETURN_THROWS();
} else {
RETURN_FALSE;
}
} else if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Passed array does not specify %s %smethod (%s)", alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
zend_type_error("Passed array does not specify %s %smethod (%s)",
alfi.func_ptr ? "a callable" : "an existing", !obj_ptr ? "static " : "", error);
}
if (error) {
efree(error);
}
zend_string_release_ex(func_name, 0);
RETURN_FALSE;

if (do_throw) {
RETURN_THROWS();
} else {
RETURN_FALSE;
}
} else if (Z_TYPE_P(zcallable) == IS_STRING) {
if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Function '%s' not %s (%s)", ZSTR_VAL(func_name), alfi.func_ptr ? "callable" : "found", error);
zend_type_error("Function '%s' not %s (%s)", ZSTR_VAL(func_name), alfi.func_ptr ? "callable" : "found", error);
}
if (error) {
efree(error);
}
zend_string_release_ex(func_name, 0);
RETURN_FALSE;

if (do_throw) {
RETURN_THROWS();
} else {
RETURN_FALSE;
}
} else {
if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Illegal value passed (%s)", error);
zend_type_error("Expected callable as first argument (%s)", error);
}
if (error) {
efree(error);
}
zend_string_release_ex(func_name, 0);
RETURN_FALSE;

if (do_throw) {
RETURN_THROWS();
} else {
RETURN_FALSE;
}
}
} else if (fcc.function_handler->type == ZEND_INTERNAL_FUNCTION &&
fcc.function_handler->internal_function.handler == zif_spl_autoload_call) {
if (do_throw) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Function spl_autoload_call() cannot be registered");
zend_throw_error(NULL, "Function spl_autoload_call() cannot be registered");
}
if (error) {
efree(error);
}
zend_string_release_ex(func_name, 0);
RETURN_FALSE;

if (do_throw) {
RETURN_THROWS();
} else {
RETURN_FALSE;
}
}
alfi.ce = fcc.calling_scope;
alfi.func_ptr = fcc.function_handler;
Expand Down Expand Up @@ -688,14 +714,14 @@ PHP_FUNCTION(spl_autoload_unregister)
}

if (!zend_is_callable_ex(zcallable, NULL, IS_CALLABLE_CHECK_SYNTAX_ONLY, &func_name, &fcc, &error)) {
zend_throw_exception_ex(spl_ce_LogicException, 0, "Unable to unregister invalid function (%s)", error);
zend_throw_error(NULL, "Unable to unregister invalid function (%s)", error);
if (error) {
efree(error);
}
if (func_name) {
zend_string_release_ex(func_name, 0);
}
RETURN_FALSE;
RETURN_THROWS();
}
obj_ptr = fcc.object;
if (error) {
Expand Down
75 changes: 47 additions & 28 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object *
}

if ((type == BP_VAR_W || type == BP_VAR_RW) && intern->nApplyCount > 0) {
zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
return &EG(error_zval);
}

Expand Down Expand Up @@ -386,9 +386,8 @@ static zval *spl_array_get_dimension_ptr(int check_inherited, spl_array_object *
ZVAL_DEREF(offset);
goto try_again;
default:
zend_error(E_WARNING, "Illegal offset type");
return (type == BP_VAR_W || type == BP_VAR_RW) ?
&EG(error_zval) : &EG(uninitialized_zval);
zend_type_error("Must be int|string, %s given", zend_zval_type_name(offset));
return NULL;
}
} /* }}} */

Expand Down Expand Up @@ -446,7 +445,8 @@ static zval *spl_array_read_dimension(zend_object *object, zval *offset, int typ
return spl_array_read_dimension_ex(1, object, offset, type, rv);
} /* }}} */

static void spl_array_write_dimension_ex(int check_inherited, zend_object *object, zval *offset, zval *value) /* {{{ */
static void spl_array_write_dimension_ex(int check_inherited, zend_object *object,
zval *offset, zval *value, uint32_t offset_arg_num) /* {{{ */
{
spl_array_object *intern = spl_array_from_obj(object);
zend_long index;
Expand All @@ -467,7 +467,7 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
}

if (intern->nApplyCount > 0) {
zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
return;
}

Expand Down Expand Up @@ -510,15 +510,23 @@ static void spl_array_write_dimension_ex(int check_inherited, zend_object *objec
ZVAL_DEREF(offset);
goto try_again;
default:
zend_error(E_WARNING, "Illegal offset type");
zval_ptr_dtor(value);
/* If offset_arg_num is 0 this mean the offset comes from an index use in [offset] */
if (offset_arg_num == 0) {
zend_type_error("Offsets must be int|string, %s given",
zend_zval_type_name(offset));
} else {
zend_argument_type_error(offset_arg_num, "must be int|string, %s given",
zend_zval_type_name(offset));
}
return;
}
} /* }}} */

static void spl_array_write_dimension(zend_object *object, zval *offset, zval *value) /* {{{ */
{
spl_array_write_dimension_ex(1, object, offset, value);
/* Pass 0 as offset arg position to inform it comes from this handler */
spl_array_write_dimension_ex(1, object, offset, value, 0);
} /* }}} */

static void spl_array_unset_dimension_ex(int check_inherited, zend_object *object, zval *offset) /* {{{ */
Expand All @@ -535,7 +543,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
}

if (intern->nApplyCount > 0) {
zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
return;
}

Expand Down Expand Up @@ -596,7 +604,7 @@ static void spl_array_unset_dimension_ex(int check_inherited, zend_object *objec
ZVAL_DEREF(offset);
goto try_again;
default:
zend_error(E_WARNING, "Illegal offset type");
zend_type_error("Must be int|string, %s given", zend_zval_type_name(offset));
return;
}
} /* }}} */
Expand Down Expand Up @@ -672,7 +680,7 @@ static int spl_array_has_dimension_ex(int check_inherited, zend_object *object,
ZVAL_DEREF(offset);
goto try_again;
default:
zend_error(E_WARNING, "Illegal offset type");
zend_type_error("Must be int|string, %s given", zend_zval_type_name(offset));
return 0;
}

Expand Down Expand Up @@ -733,7 +741,7 @@ SPL_METHOD(Array, offsetSet)
if (zend_parse_parameters(ZEND_NUM_ARGS(), "zz", &index, &value) == FAILURE) {
RETURN_THROWS();
}
spl_array_write_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, value);
spl_array_write_dimension_ex(0, Z_OBJ_P(ZEND_THIS), index, value, 1);
} /* }}} */

void spl_array_iterator_append(zval *object, zval *append_value) /* {{{ */
Expand Down Expand Up @@ -1103,9 +1111,14 @@ static void spl_array_it_rewind(zend_object_iterator *iter) /* {{{ */
/* }}} */

/* {{{ spl_array_set_array */
static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array, zend_long ar_flags, int just_array) {
static void spl_array_set_array(zval *object, spl_array_object *intern, zval *array,
zend_long ar_flags, int just_array, uint32_t arg_num) {
if (Z_TYPE_P(array) != IS_OBJECT && Z_TYPE_P(array) != IS_ARRAY) {
zend_throw_exception(spl_ce_InvalidArgumentException, "Passed variable is not an array or object", 0);
if (arg_num == 0) {
zend_type_error("Passed variable must be array|object, %s given", zend_zval_type_name(array));
} else {
zend_argument_type_error(arg_num, "must be array|object, %s given", zend_zval_type_name(array));
}
return;
}

Expand Down Expand Up @@ -1134,9 +1147,13 @@ static void spl_array_set_array(zval *object, spl_array_object *intern, zval *ar
} else {
zend_object_get_properties_t handler = Z_OBJ_HANDLER_P(array, get_properties);
if (handler != zend_std_get_properties) {
zend_throw_exception_ex(spl_ce_InvalidArgumentException, 0,
"Overloaded object of type %s is not compatible with %s",
ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
if (arg_num == 0) {
zend_type_error("Overloaded object of type %s must be compatible with %s",
ZSTR_VAL(Z_OBJCE_P(array)->name), ZSTR_VAL(intern->std.ce->name));
} else {
zend_argument_type_error(arg_num, "must be compatible with %s",
ZSTR_VAL(intern->std.ce->name));
}
return;
}
zval_ptr_dtor(&intern->array);
Expand Down Expand Up @@ -1167,7 +1184,7 @@ zend_object_iterator *spl_array_get_iterator(zend_class_entry *ce, zval *object,
spl_array_object *array_object = Z_SPLARRAY_P(object);

if (by_ref && (array_object->ar_flags & SPL_ARRAY_OVERLOADED_CURRENT)) {
zend_throw_exception(spl_ce_RuntimeException, "An iterator cannot be used with foreach by reference", 0);
zend_throw_error(NULL, "An iterator cannot be used with foreach by reference");
return NULL;
}

Expand Down Expand Up @@ -1211,7 +1228,7 @@ SPL_METHOD(Array, __construct)

ar_flags &= ~SPL_ARRAY_INT_MASK;

spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1, 1);
}
/* }}} */

Expand All @@ -1236,7 +1253,7 @@ SPL_METHOD(ArrayIterator, __construct)

ar_flags &= ~SPL_ARRAY_INT_MASK;

spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1);
spl_array_set_array(object, intern, array, ar_flags, ZEND_NUM_ARGS() == 1, 1);
}
/* }}} */

Expand Down Expand Up @@ -1315,12 +1332,12 @@ SPL_METHOD(Array, exchangeArray)
}

if (intern->nApplyCount > 0) {
zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
return;
}

RETVAL_ARR(zend_array_dup(spl_array_get_hash_table(intern)));
spl_array_set_array(object, intern, array, 0L, 1);
spl_array_set_array(object, intern, array, 0L, 1, 1);
}
/* }}} */

Expand Down Expand Up @@ -1380,7 +1397,8 @@ SPL_METHOD(Array, seek)
return; /* ok */
}
}
zend_throw_exception_ex(spl_ce_OutOfBoundsException, 0, "Seek position " ZEND_LONG_FMT " is out of range", opos);
zend_argument_value_error(1, "position " ZEND_LONG_FMT " is out of range", opos);
RETURN_THROWS();
} /* }}} */

static zend_long spl_array_object_count_elements_helper(spl_array_object *intern) /* {{{ */
Expand Down Expand Up @@ -1455,7 +1473,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
intern->nApplyCount--;
} else if (use_arg == SPL_ARRAY_METHOD_MAY_USER_ARG) {
if (zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) {
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects one argument at most", 0);
zend_argument_count_error("Function expects one argument at most");
goto exit;
}
if (arg) {
Expand All @@ -1466,7 +1484,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam
intern->nApplyCount--;
} else {
if (ZEND_NUM_ARGS() != 1 || zend_parse_parameters_ex(ZEND_PARSE_PARAMS_QUIET, ZEND_NUM_ARGS(), "z", &arg) == FAILURE) {
zend_throw_exception(spl_ce_BadMethodCallException, "Function expects exactly one argument", 0);
zend_argument_count_error("Function expects exactly one argument");
goto exit;
}
ZVAL_COPY_VALUE(&params[1], arg);
Expand Down Expand Up @@ -1732,7 +1750,7 @@ SPL_METHOD(Array, unserialize)
}

if (intern->nApplyCount > 0) {
zend_error(E_WARNING, "Modification of ArrayObject during sorting is prohibited");
zend_throw_error(NULL, "Modification of ArrayObject during sorting is prohibited");
return;
}

Expand Down Expand Up @@ -1788,7 +1806,7 @@ SPL_METHOD(Array, unserialize)
ZVAL_NULL(array);
SEPARATE_ARRAY(&intern->array);
} else {
spl_array_set_array(object, intern, array, 0L, 1);
spl_array_set_array(object, intern, array, 0L, 1, 0);
}

if (*p != ';') {
Expand Down Expand Up @@ -1817,6 +1835,7 @@ SPL_METHOD(Array, unserialize)

outexcept:
PHP_VAR_UNSERIALIZE_DESTROY(var_hash);
/* TODO Use standard Error? */
zend_throw_exception_ex(spl_ce_UnexpectedValueException, 0, "Error at offset " ZEND_LONG_FMT " of %zd bytes", (zend_long)((char*)p - buf), buf_len);
RETURN_THROWS();

Expand Down Expand Up @@ -1884,7 +1903,7 @@ SPL_METHOD(Array, __unserialize)
zval_ptr_dtor(&intern->array);
ZVAL_UNDEF(&intern->array);
} else {
spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1);
spl_array_set_array(ZEND_THIS, intern, storage_zv, 0L, 1, 0);
}

object_properties_load(&intern->std, Z_ARRVAL_P(members_zv));
Expand Down
Loading