Repository: box-project/box (php)

Issue:
memory_to_bytes() INI parsing issue (32bit)
## Bug report

| Question    | Answer
| ------------| ---------------
| Box version | Box version 3.13.0@275b091
| PHP version | PHP 7.4.18 (cli) (built: Apr 27 2021 17:19:39) ( ZTS Visual C++ 2017 x86 )
| Platform with version | Windows 10
| Github Repo | NA

To replicate: 
Run **box.phar compile** with [**2G**](https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes) as PHP [memory_limit](https://www.php.net/manual/en/ini.core.php#ini.memory-limit) INI setting.

Error:

PHP Fatal error:  Uncaught TypeError: Return value of _HumbugBox113887eee2b6\KevinGH\Box\memory_to_bytes() must be of the type int, float returned in phar://box.phar/src/functions.php:81
Stack trace:
0 phar://box.phar/src/Console/Php/PhpSettingsHandler.php(62): _HumbugBox113887eee2b6\KevinGH\Box\memory_to_bytes('2G')
1 phar://box.phar/src/Console/Php/PhpSettingsHandler.php(33): _HumbugBox113887eee2b6\KevinGH\Box\Console\Php\PhpSettingsHandler->bumpMemoryLimit()
2 phar://box.phar/src/functions.php(124): _HumbugBox113887eee2b6\KevinGH\Box\Console\Php\PhpSettingsHandler->check()
3 phar://box.phar/src/Console/Command/Compile.php(105): _HumbugBox113887eee2b6\KevinGH\Box\check_php_settings(Object(_HumbugBox113887eee2b6\Kevin in phar://box.phar/src/functions.php on line 81


Pull request:
Fix integer overflow
Closes #556

Patch:
diff --git a/src/functions.php b/src/functions.php
index 978456da..8a547923 100644
--- a/src/functions.php
+++ b/src/functions.php
@@ -25,6 +25,8 @@ use function defined;
 use ErrorException;
 use function floor;
 use function function_exists;
+use function is_float;
+use function is_int;
 use KevinGH\Box\Console\IO\IO;
 use KevinGH\Box\Console\Php\PhpSettingsHandler;
 use function KevinGH\Box\FileSystem\copy;
@@ -114,9 +116,13 @@ function get_phar_signing_algorithms(): array
 
 /**
  * @private
+ *
+ * @param float|int $size
  */
-function format_size(int $size, int $decimals = 2): string
+function format_size($size, int $decimals = 2): string
 {
+    Assert::true(is_int($size) || is_float($size));
+
     if (-1 === $size) {
         return '-1';
     }
@@ -139,8 +145,10 @@ function format_size(int $size, int $decimals = 2): string
 
 /**
  * @private
+ *
+ * @return float|int
  */
-function memory_to_bytes(string $value): int
+function memory_to_bytes(string $value)
 {
     $unit = strtolower($value[strlen($value) - 1]);
 


Test patch:
diff --git a/tests/FunctionsTest.php b/tests/FunctionsTest.php
index 1004c7bb..70289c08 100644
--- a/tests/FunctionsTest.php
+++ b/tests/FunctionsTest.php
@@ -62,8 +62,10 @@ class FunctionsTest extends TestCase
 
     /**
      * @dataProvider provideBytes
+     *
+     * @param float|int $bytes
      */
-    public function test_it_can_format_bytes(int $bytes, string $expected): void
+    public function test_it_can_format_bytes($bytes, string $expected): void
     {
         $actual = format_size($bytes);
 
@@ -72,8 +74,10 @@ class FunctionsTest extends TestCase
 
     /**
      * @dataProvider provideMemory
+     *
+     * @param float|int $expected
      */
-    public function test_it_can_convert_a_memory_limit_amount_to_bytes(string $memory, int $expected): void
+    public function test_it_can_convert_a_memory_limit_amount_to_bytes(string $memory, $expected): void
     {
         $actual = memory_to_bytes($memory);
 
@@ -103,6 +107,7 @@ class FunctionsTest extends TestCase
         yield [1024 ** 4, '1.00TB'];
         yield [1024 ** 5, '1.00PB'];
         yield [1024 ** 6, '1.00EB'];
+        yield [1.073741824E+21, '931.32EB'];
     }
 
     public function provideMemory(): Generator
@@ -115,5 +120,7 @@ class FunctionsTest extends TestCase
         yield ['10m', (1024 ** 2) * 10];
         yield ['1g', 1024 ** 3];
         yield ['10g', (1024 ** 3) * 10];
+        yield ['10g', (1024 ** 3) * 10];
+        yield ['1000000000000g', 1.073741824E+21];
     }
 }