From 54c12ee55aad5be7b844f8d74b29d9a702b4b9bc Mon Sep 17 00:00:00 2001 From: Alexander Wozniak Date: Mon, 30 Nov 2020 13:02:00 +0100 Subject: [PATCH] Ensured non-negative values for bcmath Beginning with PHP8 supplying a negative value as an argument for scale in the bcmath functions will generate a ValueError. Negative values supplied to the Zend math functions are ignored and 0 is used generating the same results as before. --- .../zend-locale/library/Zend/Locale/Math.php | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/zend-locale/library/Zend/Locale/Math.php b/packages/zend-locale/library/Zend/Locale/Math.php index 1f7ae2e66..41af42841 100644 --- a/packages/zend-locale/library/Zend/Locale/Math.php +++ b/packages/zend-locale/library/Zend/Locale/Math.php @@ -216,11 +216,12 @@ public static function localize($value) * Fixes a problem of BCMath with numbers containing exponents * * @param integer $value Value to erase the exponent - * @param integer $scale (Optional) Scale to use + * @param int|null $scale (Optional) Scale to use * @return string */ public static function exponent($value, $scale = null) { + $scale = max(0, $scale); if (!extension_loaded('bcmath')) { return $value; } @@ -242,11 +243,18 @@ public static function exponent($value, $scale = null) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Add($op1, $op2, $scale = null) { + /** + * Before PHP8 bcmath functions could be called with a negative + * scale factor which was handled as if $scale were 0 + * + * With PHP8 this would emit a ValueError + */ + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); @@ -258,11 +266,12 @@ public static function Add($op1, $op2, $scale = null) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Sub($op1, $op2, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); return bcsub($op1, $op2, $scale); @@ -273,11 +282,12 @@ public static function Sub($op1, $op2, $scale = null) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Pow($op1, $op2, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); return bcpow($op1, $op2, $scale); @@ -288,11 +298,12 @@ public static function Pow($op1, $op2, $scale = null) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Mul($op1, $op2, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); return bcmul($op1, $op2, $scale); @@ -303,11 +314,12 @@ public static function Mul($op1, $op2, $scale = null) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Div($op1, $op2, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); return bcdiv($op1, $op2, $scale); @@ -317,11 +329,12 @@ public static function Div($op1, $op2, $scale = null) * BCSqrt - fixes a problem of BCMath and exponential numbers * * @param string $op1 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Sqrt($op1, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); return bcsqrt($op1, $scale); } @@ -345,11 +358,12 @@ public static function Mod($op1, $op2) * * @param string $op1 * @param string $op2 - * @param integer $scale + * @param int|null $scale * @return string */ public static function Comp($op1, $op2, $scale = null) { + $scale = max(0, $scale); $op1 = self::exponent($op1, $scale); $op2 = self::exponent($op2, $scale); return bccomp($op1, $op2, $scale);