]> granicus.if.org Git - php/commitdiff
make bcpowmod stricter by not returning false, instead throw exception
authorVladyslav Startsev <17382248+vladyslavstartsev@users.noreply.github.com>
Sat, 20 Jun 2020 15:26:51 +0000 (18:26 +0300)
committerMáté Kocsis <kocsismate@woohoolabs.com>
Mon, 22 Jun 2020 07:31:55 +0000 (09:31 +0200)
Closes GH-5747

ext/bcmath/bcmath.c
ext/bcmath/bcmath.stub.php
ext/bcmath/bcmath_arginfo.h
ext/bcmath/libbcmath/src/raisemod.c
ext/bcmath/tests/bcpowmod_negative_exponent.phpt [new file with mode: 0644]
ext/bcmath/tests/bcpowmod_zero_modulus.phpt [moved from ext/bcmath/tests/bcpowmod_error4.phpt with 50% similarity]
ext/opcache/Optimizer/zend_func_info.c

index 1b91fba72fe741707d62df2d221d115363c2d5aa..f1e298b5cc9b07b3eeb9d5237bb6e484207ad059 100644 (file)
@@ -404,10 +404,16 @@ PHP_FUNCTION(bcpowmod)
        php_str2num(&second, ZSTR_VAL(right));
        php_str2num(&mod, ZSTR_VAL(modulus));
 
-       if (bc_raisemod(first, second, mod, &result, scale) != -1) {
-               RETVAL_STR(bc_num2str_ex(result, scale));
-       } else {
-               RETVAL_FALSE;
+       switch (bc_raisemod(first, second, mod, &result, scale)) {
+               case 0:
+                       RETVAL_STR(bc_num2str_ex(result, scale));
+                       break;
+               case -1:
+                       zend_throw_exception_ex(zend_ce_division_by_zero_error, 0, "Modulo by zero");
+                       break;
+               case -2:
+                       zend_argument_value_error(2, "must be greater than 0");
+                       break;
        }
 
        bc_free_num(&first);
index 45e75376f0991105b2dbd357b2d4434146a472c0..97232b0ed712131337f91d7394ea453deaf7d1d1 100644 (file)
@@ -12,7 +12,7 @@ function bcdiv(string $dividend, string $divisor, ?int $scale = null): string {}
 
 function bcmod(string $dividend, string $divisor, ?int $scale = null): string {}
 
-function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string|false {}
+function bcpowmod(string $base, string $exponent, string $modulus, ?int $scale = null): string {}
 
 function bcpow(string $base, string $exponent, ?int $scale = null): string {}
 
index 75f864dc9d36f2cecc957db2b83632b32467d743..46fc5ff9bd3e27a88131747b62a8396d787d7821 100644 (file)
@@ -18,7 +18,7 @@ ZEND_END_ARG_INFO()
 
 #define arginfo_bcmod arginfo_bcdiv
 
-ZEND_BEGIN_ARG_WITH_RETURN_TYPE_MASK_EX(arginfo_bcpowmod, 0, 3, MAY_BE_STRING|MAY_BE_FALSE)
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_bcpowmod, 0, 3, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, base, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, exponent, IS_STRING, 0)
        ZEND_ARG_TYPE_INFO(0, modulus, IS_STRING, 0)
index 286590394095f5da4b453e57ea560a607846055f..46dfd7a8a863a1043676be9dc2cd14f85adc32a0 100644 (file)
@@ -67,7 +67,7 @@ bc_raisemod (bc_num base, bc_num expo, bc_num mod, bc_num *result, int scale)
 
   /* Check for correct numbers. */
   if (bc_is_zero(mod)) return -1;
-  if (bc_is_neg(expo)) return -1;
+  if (bc_is_neg(expo)) return -2;
 
   /* Set initial values.  */
   power = bc_copy_num (base);
diff --git a/ext/bcmath/tests/bcpowmod_negative_exponent.phpt b/ext/bcmath/tests/bcpowmod_negative_exponent.phpt
new file mode 100644 (file)
index 0000000..a72b395
--- /dev/null
@@ -0,0 +1,16 @@
+--TEST--
+bc_raisemod's expo can't be negative
+--CREDITS--
+Gabriel Caruso (carusogabriel34@gmail.com)
+--SKIPIF--
+<?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
+--FILE--
+<?php
+try {
+    var_dump(bcpowmod('1', '-1', '2'));
+} catch (\ValueError $e) {
+    echo $e->getMessage() . \PHP_EOL;
+}
+?>
+--EXPECT--
+bcpowmod(): Argument #2 ($exponent) must be greater than 0
similarity index 50%
rename from ext/bcmath/tests/bcpowmod_error4.phpt
rename to ext/bcmath/tests/bcpowmod_zero_modulus.phpt
index 7c2ba5012b9f5892d3df7cb86abd9e5c2954fb34..0b810969c6a79cf886badaf9f560e37eac87e3dc 100644 (file)
@@ -1,10 +1,16 @@
 --TEST--
-bc_raisemod's mod can't be zero and expo can't be negative
+bc_raisemod's mod can't be zero
 --CREDITS--
 Gabriel Caruso (carusogabriel34@gmail.com)
 --SKIPIF--
 <?php if(!extension_loaded('bcmath')) die('skip bcmath extension not loaded'); ?>
 --FILE--
-<?php var_dump(bcpowmod('1', '-1', '0')); ?>
+<?php
+try {
+    var_dump(bcpowmod('1', '-1', '0'));
+} catch (DivisionByZeroError $ex) {
+    echo $ex->getMessage(), PHP_EOL;
+}
+?>
 --EXPECT--
-bool(false)
+Modulo by zero
index ff985fc63fae619bf329a1ee82f9eee3e1c4781b..1b9e800d7d4ccfcee1c21e414547d8615602190d 100644 (file)
@@ -789,7 +789,7 @@ static const func_info_t func_infos[] = {
        F1("bcmul",                                                                     MAY_BE_STRING),
        F1("bcdiv",                                                                     MAY_BE_STRING),
        F1("bcmod",                                                                     MAY_BE_STRING),
-       F1("bcpowmod",                                                          MAY_BE_FALSE | MAY_BE_STRING),
+       F1("bcpowmod",                                                          MAY_BE_STRING),
        F1("bcpow",                                                                     MAY_BE_STRING),
        F1("bcsqrt",                                                            MAY_BE_STRING),