Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix integer overflow #561

Merged
merged 3 commits into from
Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
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;
Expand Down Expand Up @@ -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';
}
Expand All @@ -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]);

Expand Down
11 changes: 9 additions & 2 deletions tests/FunctionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ public function test_it_can_provide_the_phars_algorithm_extensions(int $algorith

/**
* @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);

Expand All @@ -72,8 +74,10 @@ public function test_it_can_format_bytes(int $bytes, string $expected): void

/**
* @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);

Expand Down Expand Up @@ -103,6 +107,7 @@ public function provideBytes(): Generator
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
Expand All @@ -115,5 +120,7 @@ public function provideMemory(): Generator
yield ['10m', (1024 ** 2) * 10];
yield ['1g', 1024 ** 3];
yield ['10g', (1024 ** 3) * 10];
yield ['10g', (1024 ** 3) * 10];
yield ['1000000000000g', 1.073741824E+21];
}
}