Skip to content

Commit

Permalink
Throw correct exception from ArrayObject sort methods
Browse files Browse the repository at this point in the history
Let normal zpp throw ArgumentCountErrors.
  • Loading branch information
nikic committed Jul 17, 2020
1 parent 81d8d60 commit 1cba736
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
10 changes: 6 additions & 4 deletions ext/spl/spl_array.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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(&params[1], arg);
Expand Down
16 changes: 10 additions & 6 deletions ext/spl/tests/arrayObject_natcasesort_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -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"
}
}
16 changes: 10 additions & 6 deletions ext/spl/tests/arrayObject_natsort_basic1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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--
Expand All @@ -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"
}
}
8 changes: 4 additions & 4 deletions ext/spl/tests/arrayObject_uasort_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 4 additions & 4 deletions ext/spl/tests/arrayObject_uksort_error1.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 1cba736

Please sign in to comment.