From: Nikita Popov Date: Fri, 17 Jul 2020 08:34:49 +0000 (+0200) Subject: Throw correct exception from ArrayObject sort methods X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1cba736470fbe40dffb611c1b351f729f2c469a3;p=php Throw correct exception from ArrayObject sort methods Let normal zpp throw ArgumentCountErrors. --- diff --git a/ext/spl/spl_array.c b/ext/spl/spl_array.c index f8039c64af..a2dfff3482 100644 --- a/ext/spl/spl_array.c +++ b/ext/spl/spl_array.c @@ -1422,12 +1422,15 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam GC_ADDREF(aht); if (!use_arg) { + if (zend_parse_parameters_none() == FAILURE) { + goto exit; + } + intern->nApplyCount++; call_user_function(EG(function_table), NULL, &function_name, return_value, 1, params); 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); + if (zend_parse_parameters(ZEND_NUM_ARGS(), "|z", &arg) == FAILURE) { goto exit; } if (arg) { @@ -1437,8 +1440,7 @@ static void spl_array_method(INTERNAL_FUNCTION_PARAMETERS, char *fname, int fnam call_user_function(EG(function_table), NULL, &function_name, return_value, arg ? 2 : 1, params); 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); + if (zend_parse_parameters(ZEND_NUM_ARGS(), "z", &arg) == FAILURE) { goto exit; } ZVAL_COPY_VALUE(¶ms[1], arg); diff --git a/ext/spl/tests/arrayObject_natcasesort_basic1.phpt b/ext/spl/tests/arrayObject_natcasesort_basic1.phpt index 5b3530b8d4..6cffe4a621 100644 --- a/ext/spl/tests/arrayObject_natcasesort_basic1.phpt +++ b/ext/spl/tests/arrayObject_natcasesort_basic1.phpt @@ -13,7 +13,11 @@ $ao1 = new ArrayObject(array('boo10','boo1','boo2','boo22','BOO5')); $ao2 = new ArrayObject(array('a'=>'boo10','b'=>'boo1','c'=>'boo2','d'=>'boo22','e'=>'BOO5')); var_dump($ao1->natcasesort()); var_dump($ao1); -var_dump($ao2->natcasesort('blah')); +try { + var_dump($ao2->natcasesort('blah')); +} catch (ArgumentCountError $e) { + echo $e->getMessage(), "\n"; +} var_dump($ao2); ?> --EXPECT-- @@ -34,19 +38,19 @@ object(ArrayObject)#1 (1) { string(5) "boo22" } } -bool(true) +ArrayObject::natcasesort() expects exactly 0 parameters, 1 given object(ArrayObject)#2 (1) { ["storage":"ArrayObject":private]=> array(5) { + ["a"]=> + string(5) "boo10" ["b"]=> string(4) "boo1" ["c"]=> string(4) "boo2" - ["e"]=> - string(4) "BOO5" - ["a"]=> - string(5) "boo10" ["d"]=> string(5) "boo22" + ["e"]=> + string(4) "BOO5" } } diff --git a/ext/spl/tests/arrayObject_natsort_basic1.phpt b/ext/spl/tests/arrayObject_natsort_basic1.phpt index c8c789f038..953e55ca28 100644 --- a/ext/spl/tests/arrayObject_natsort_basic1.phpt +++ b/ext/spl/tests/arrayObject_natsort_basic1.phpt @@ -13,7 +13,11 @@ $ao1 = new ArrayObject(array('boo10','boo1','boo2','boo22','BOO5')); $ao2 = new ArrayObject(array('a'=>'boo10','b'=>'boo1','c'=>'boo2','d'=>'boo22','e'=>'BOO5')); var_dump($ao1->natsort()); var_dump($ao1); -var_dump($ao2->natsort('blah')); +try { + var_dump($ao2->natsort('blah')); +} catch (ArgumentCountError $e) { + echo $e->getMessage(), "\n"; +} var_dump($ao2); ?> --EXPECT-- @@ -34,19 +38,19 @@ object(ArrayObject)#1 (1) { string(5) "boo22" } } -bool(true) +ArrayObject::natsort() expects exactly 0 parameters, 1 given object(ArrayObject)#2 (1) { ["storage":"ArrayObject":private]=> array(5) { - ["e"]=> - string(4) "BOO5" + ["a"]=> + string(5) "boo10" ["b"]=> string(4) "boo1" ["c"]=> string(4) "boo2" - ["a"]=> - string(5) "boo10" ["d"]=> string(5) "boo22" + ["e"]=> + string(4) "BOO5" } } diff --git a/ext/spl/tests/arrayObject_uasort_error1.phpt b/ext/spl/tests/arrayObject_uasort_error1.phpt index 999d765e48..30a9734110 100644 --- a/ext/spl/tests/arrayObject_uasort_error1.phpt +++ b/ext/spl/tests/arrayObject_uasort_error1.phpt @@ -11,16 +11,16 @@ $ao = new ArrayObject(); try { $ao->uasort(); -} catch (BadMethodCallException $e) { +} catch (ArgumentCountError $e) { echo $e->getMessage() . "\n"; } try { $ao->uasort(1,2); -} catch (BadMethodCallException $e) { +} catch (ArgumentCountError $e) { echo $e->getMessage() . "\n"; } ?> --EXPECT-- -Function expects exactly one argument -Function expects exactly one argument +ArrayObject::uasort() expects exactly 1 parameter, 0 given +ArrayObject::uasort() expects exactly 1 parameter, 2 given diff --git a/ext/spl/tests/arrayObject_uksort_error1.phpt b/ext/spl/tests/arrayObject_uksort_error1.phpt index 15d47fc95a..d1f5d5a650 100644 --- a/ext/spl/tests/arrayObject_uksort_error1.phpt +++ b/ext/spl/tests/arrayObject_uksort_error1.phpt @@ -11,16 +11,16 @@ $ao = new ArrayObject(); try { $ao->uksort(); -} catch (BadMethodCallException $e) { +} catch (ArgumentCountError $e) { echo $e->getMessage() . "\n"; } try { $ao->uksort(1,2); -} catch (BadMethodCallException $e) { +} catch (ArgumentCountError $e) { echo $e->getMessage() . "\n"; } ?> --EXPECT-- -Function expects exactly one argument -Function expects exactly one argument +ArrayObject::uksort() expects exactly 1 parameter, 0 given +ArrayObject::uksort() expects exactly 1 parameter, 2 given