From b3ae29d4f8097131580ab38e27e7630300e2c9fe Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Mon, 16 Sep 2013 11:12:56 +0200 Subject: [PATCH] fixed bytes conversion when used on 32-bits systems --- .../Extension/Validator/Util/ServerParams.php | 23 +++++++++++------ .../HttpFoundation/File/UploadedFile.php | 25 +++++++++++++------ .../DataCollector/MemoryDataCollector.php | 23 +++++++++++------ 3 files changed, 49 insertions(+), 22 deletions(-) diff --git a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php index fab6ac2a61..a20bf5ad64 100644 --- a/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php +++ b/src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php @@ -23,20 +23,29 @@ class ServerParams */ public function getPostMaxSize() { - $iniMax = $this->getNormalizedIniPostMaxSize(); + $iniMax = strtolower($this->getNormalizedIniPostMaxSize()); if ('' === $iniMax) { return null; } - if (preg_match('#^\+?(0X?)?(.*?)([KMG]?)$#', $iniMax, $match)) { - $shifts = array('' => 0, 'K' => 10, 'M' => 20, 'G' => 30); - $bases = array('' => 10, '0' => 8, '0X' => 16); - - return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]]; + $max = ltrim($iniMax, '+'); + if (0 === strpos($max, '0x')) { + $max = intval($max, 16); + } elseif (0 === strpos($max, '0')) { + $max = intval($max, 8); + } else { + $max = intval($max); } - return 0; + switch (substr($iniMax, -1)) { + case 't': $max *= 1024; + case 'g': $max *= 1024; + case 'm': $max *= 1024; + case 'k': $max *= 1024; + } + + return $max; } /** diff --git a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php index 1f23c35c6b..9591672baa 100644 --- a/src/Symfony/Component/HttpFoundation/File/UploadedFile.php +++ b/src/Symfony/Component/HttpFoundation/File/UploadedFile.php @@ -262,20 +262,29 @@ class UploadedFile extends File */ public static function getMaxFilesize() { - $max = strtolower(ini_get('upload_max_filesize')); + $iniMax = strtolower(ini_get('upload_max_filesize')); - if ('' === $max) { + if ('' === $iniMax) { return PHP_INT_MAX; } - if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $max, $match)) { - $shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30); - $bases = array('' => 10, '0' => 8, '0x' => 16); - - return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]]; + $max = ltrim($iniMax, '+'); + if (0 === strpos($max, '0x')) { + $max = intval($max, 16); + } elseif (0 === strpos($max, '0')) { + $max = intval($max, 8); + } else { + $max = intval($max); } - return 0; + switch (substr($iniMax, -1)) { + case 't': $max *= 1024; + case 'g': $max *= 1024; + case 'm': $max *= 1024; + case 'k': $max *= 1024; + } + + return $max; } /** diff --git a/src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php b/src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php index 5540a1b27a..115101f7cc 100644 --- a/src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php +++ b/src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php @@ -25,7 +25,7 @@ class MemoryDataCollector extends DataCollector { $this->data = array( 'memory' => 0, - 'memory_limit' => $this->convertToBytes(strtolower(ini_get('memory_limit'))), + 'memory_limit' => $this->convertToBytes(ini_get('memory_limit')), ); } @@ -79,13 +79,22 @@ class MemoryDataCollector extends DataCollector return -1; } - if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $memoryLimit, $match)) { - $shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30); - $bases = array('' => 10, '0' => 8, '0x' => 16); - - return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]]; + $max = strtolower(ltrim($memoryLimit, '+')); + if (0 === strpos($max, '0x')) { + $max = intval($max, 16); + } elseif (0 === strpos($max, '0')) { + $max = intval($max, 8); + } else { + $max = intval($max); } - return 0; + switch (substr($memoryLimit, -1)) { + case 't': $max *= 1024; + case 'g': $max *= 1024; + case 'm': $max *= 1024; + case 'k': $max *= 1024; + } + + return $max; } }