From 414aa4fbc23cba1c9a685bb267c253b56f7454e4 Mon Sep 17 00:00:00 2001 From: Sebastian Mendel Date: Wed, 14 Mar 2007 12:47:37 +0000 Subject: [PATCH] bug #1676033 [core] pow(int,int) causes overflow --- ChangeLog | 1 + libraries/common.lib.php | 75 ++++++++++++++++++++++++++++----- libraries/ip_allow_deny.lib.php | 2 +- 3 files changed, 67 insertions(+), 11 deletions(-) diff --git a/ChangeLog b/ChangeLog index 09ca6397c..0c2b333b2 100644 --- a/ChangeLog +++ b/ChangeLog @@ -12,6 +12,7 @@ $HeadURL$ - bug #1671403 [parser] using "client" as table name - bug #1672379 [core] Call to undefined function PMA_removeCookie() . [general] use PMA_getenv('PHP_SELF') +- bug #1676033 [core] pow(int,int) causes overflow 2.10.0.3 (not released yet) ===================== diff --git a/libraries/common.lib.php b/libraries/common.lib.php index 9b622d3f3..16daae8e3 100644 --- a/libraries/common.lib.php +++ b/libraries/common.lib.php @@ -454,6 +454,61 @@ if (!defined('PMA_MINIMUM_COMMON')) { */ require_once './libraries/js_escape.lib.php'; + /** + * Exponential expression / raise number into power + * + * @uses function_exists() + * @uses bcpow() + * @uses gmp_pow() + * @uses gmp_strval() + * @uses pow() + * @param number $base + * @param number $exp + * @param string pow function use, or false for auto-detect + * @return mixed string or float + */ + function PMA_pow($base, $exp, $use_function = false) + { + static $pow_function = null; + if (null == $pow_function) { + if (function_exists('bcpow')) { + // BCMath Arbitrary Precision Mathematics Function + $pow_function = 'bcpow'; + } elseif (function_exists('gmp_pow')) { + // GMP Function + $pow_function = 'gmp_pow'; + } else { + // PHP function + $pow_function = 'pow'; + } + } + + if (! $use_function) { + $use_function = $pow_function; + } + + switch ($use_function) { + case 'bcpow' : + $pow = bcpow($base, $exp); + break; + case 'gmp_pow' : + $pow = gmp_strval(gmp_pow($base, $exp)); + break; + case 'pow' : + $base = (float) $base; + $exp = (int) $exp; + if ($exp < 0) { + return false; + } + $pow = pow($base, $exp); + break; + default: + $pow = $use_function($base, $exp); + } + + return $pow; + } + /** * string PMA_getIcon(string $icon) * @@ -1577,15 +1632,15 @@ if (typeof(window.parent) != 'undefined' */ function PMA_formatByteDown($value, $limes = 6, $comma = 0) { - $dh = pow(10, $comma); - $li = pow(10, $limes); + $dh = PMA_pow(10, $comma); + $li = PMA_pow(10, $limes); $return_value = $value; $unit = $GLOBALS['byteUnits'][0]; for ($d = 6, $ex = 15; $d >= 1; $d--, $ex-=3) { - if (isset($GLOBALS['byteUnits'][$d]) && $value >= $li * pow(10, $ex)) { + if (isset($GLOBALS['byteUnits'][$d]) && $value >= $li * PMA_pow(10, $ex)) { // use 1024.0 to avoid integer overflow on 64-bit machines - $value = round($value / (pow(1024.0, $d) / $dh)) /$dh; + $value = round($value / (PMA_pow(1024, $d) / $dh)) /$dh; $unit = $GLOBALS['byteUnits'][$d]; break 1; } // end if @@ -1668,22 +1723,22 @@ if (typeof(window.parent) != 'undefined' $sign = ''; } - $dh = pow(10, $comma); - $li = pow(10, $length); + $dh = PMA_pow(10, $comma); + $li = PMA_pow(10, $length); $unit = $units[0]; if ($value >= 1) { for ($d = 8; $d >= 0; $d--) { - if (isset($units[$d]) && $value >= $li * pow(1000.0, $d-1)) { - $value = round($value / (pow(1000.0, $d) / $dh)) /$dh; + if (isset($units[$d]) && $value >= $li * PMA_pow(1000, $d-1)) { + $value = round($value / (PMA_pow(1000, $d) / $dh)) /$dh; $unit = $units[$d]; break 1; } // end if } // end for } elseif (!$only_down && (float) $value !== 0.0) { for ($d = -8; $d <= 8; $d++) { - if (isset($units[$d]) && $value <= $li * pow(1000.0, $d-1)) { - $value = round($value / (pow(1000.0, $d) / $dh)) /$dh; + if (isset($units[$d]) && $value <= $li * PMA_pow(1000, $d-1)) { + $value = round($value / (PMA_pow(1000, $d) / $dh)) /$dh; $unit = $units[$d]; break 1; } // end if diff --git a/libraries/ip_allow_deny.lib.php b/libraries/ip_allow_deny.lib.php index 0ce7a13b4..fd5d8e87b 100644 --- a/libraries/ip_allow_deny.lib.php +++ b/libraries/ip_allow_deny.lib.php @@ -75,7 +75,7 @@ function PMA_ipMaskTest($testRange, $ipToTest) for ($i = 0; $i < 31; $i++) { if ($i < $regs[5] - 1) { - $maskl = $maskl + pow(2, (30 - $i)); + $maskl = $maskl + PMA_pow(2, (30 - $i)); } // end if } // end for