diff options
-rw-r--r-- | lib/private/MemoryInfo.php | 21 | ||||
-rw-r--r-- | lib/public/Util.php | 11 | ||||
-rw-r--r-- | tests/lib/MemoryInfoTest.php | 4 |
3 files changed, 27 insertions, 9 deletions
diff --git a/lib/private/MemoryInfo.php b/lib/private/MemoryInfo.php index ed6617d879d..266fb15a573 100644 --- a/lib/private/MemoryInfo.php +++ b/lib/private/MemoryInfo.php @@ -24,8 +24,11 @@ declare(strict_types=1); * along with this program. If not, see <http://www.gnu.org/licenses/>. * */ + namespace OC; +use OCP\Util; + /** * Helper class that covers memory info. */ @@ -45,14 +48,14 @@ class MemoryInfo { /** * Returns the php memory limit. * - * @return int The memory limit in bytes. + * @return int|float The memory limit in bytes. */ - public function getMemoryLimit(): int { + public function getMemoryLimit(): int|float { $iniValue = trim(ini_get('memory_limit')); if ($iniValue === '-1') { return -1; - } elseif (is_numeric($iniValue) === true) { - return (int)$iniValue; + } elseif (is_numeric($iniValue)) { + return Util::numericToNumber($iniValue); } else { return $this->memoryLimitToBytes($iniValue); } @@ -62,11 +65,15 @@ class MemoryInfo { * Converts the ini memory limit to bytes. * * @param string $memoryLimit The "memory_limit" ini value - * @return int */ - private function memoryLimitToBytes(string $memoryLimit): int { + private function memoryLimitToBytes(string $memoryLimit): int|float { $last = strtolower(substr($memoryLimit, -1)); - $memoryLimit = (int)substr($memoryLimit, 0, -1); + $number = substr($memoryLimit, 0, -1); + if (is_numeric($number)) { + $memoryLimit = Util::numericToNumber($number); + } else { + throw new \InvalidArgumentException($number.' is not a valid numeric string (in memory_limit ini directive)'); + } // intended fall through switch ($last) { diff --git a/lib/public/Util.php b/lib/public/Util.php index b2b3322fe86..dcb2ac4878f 100644 --- a/lib/public/Util.php +++ b/lib/public/Util.php @@ -340,6 +340,17 @@ class Util { } /** + * Converts string to int of float depending if it fits an int + * @param numeric-string $number numeric string + * @return int|float int if it fits, float if it is too big + * @since 26.0.0 + */ + public static function numericToNumber(string $number): int|float { + /* This is a hack to cast to (int|float) */ + return 0 + $number; + } + + /** * Make a human file size (2048 to 2 kB) * @param int $bytes file size in bytes * @return string a human readable file size diff --git a/tests/lib/MemoryInfoTest.php b/tests/lib/MemoryInfoTest.php index f6557eed45c..1a983f19ece 100644 --- a/tests/lib/MemoryInfoTest.php +++ b/tests/lib/MemoryInfoTest.php @@ -71,10 +71,10 @@ class MemoryInfoTest extends TestCase { * Tests that getMemoryLimit works as expected. * * @param string $iniValue The "memory_limit" ini data. - * @param int $expected The expected detected memory limit. + * @param int|float $expected The expected detected memory limit. * @dataProvider getMemoryLimitTestData */ - public function testMemoryLimit($iniValue, int $expected) { + public function testMemoryLimit(string $iniValue, int|float $expected) { ini_set('memory_limit', $iniValue); $memoryInfo = new MemoryInfo(); self::assertEquals($expected, $memoryInfo->getMemoryLimit()); |