]> granicus.if.org Git - php/commitdiff
Throw correct exception from ArrayObject sort methods
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 17 Jul 2020 08:34:49 +0000 (10:34 +0200)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 17 Jul 2020 08:46:11 +0000 (10:46 +0200)
Let normal zpp throw ArgumentCountErrors.

ext/spl/spl_array.c
ext/spl/tests/arrayObject_natcasesort_basic1.phpt
ext/spl/tests/arrayObject_natsort_basic1.phpt
ext/spl/tests/arrayObject_uasort_error1.phpt
ext/spl/tests/arrayObject_uksort_error1.phpt

index f8039c64af8858a2ba8d12c55c2f5406026f5d4d..a2dfff3482223365ad9c804af25380c11196553e 100644 (file)
@@ -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(&params[1], arg);
index 5b3530b8d422f029e639e6c60e6dff3b91633d84..6cffe4a621035a1204f127adb53f795c6bf3fd78 100644 (file)
@@ -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"
   }
 }
index c8c789f03824b8110f1e09505b300804a7760453..953e55ca2890a18b1e2f7a7650ae3e692a203779 100644 (file)
@@ -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"
   }
 }
index 999d765e48b807d2c363a2ba5f7efe87f0acc7f4..30a9734110cd0a97e4173daea3662db59afd7480 100644 (file)
@@ -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
index 15d47fc95a34e6a072cb9140ab872d011aae4465..d1f5d5a6509edd6ad411cbacec23f9e5b747ffab 100644 (file)
@@ -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