-
-
Notifications
You must be signed in to change notification settings - Fork 101
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
memory_to_bytes() INI parsing issue (32bit) #556
Comments
Hi, thanks for the bug report; is it something you can manage to reproduce consistently? The problematic function is: function memory_to_bytes(string $value): int
{
$unit = strtolower($value[strlen($value) - 1]);
$bytes = (int) $value;
switch ($unit) {
case 'g':
$bytes *= 1024;
// no break (cumulative multiplier)
case 'm':
$bytes *= 1024;
// no break (cumulative multiplier)
case 'k':
$bytes *= 1024;
}
return $bytes;
} but I hardly see how |
After a bit of reading it looks like an Integer overflow as the number goes over PHP_INT_MAX on 32bit PHP installs. Looking at the values as they are multiplied in the function as it gets to the kilobytes one the value is interpreted as a float.
Suggestion: /**
* @param string $value
* @return int
*/
function memory_to_bytes(string $value): int
{
$unit = strtolower($value[strlen($value) - 1]);
$bytes = substr($value, 0, -1);
switch ($unit) {
case 'g':
$bytes *= 1024;
// no break (cumulative multiplier)
case 'm':
$bytes *= 1024;
// no break (cumulative multiplier)
case 'k':
$bytes *= 1024;
}
// If 32bit limit memory to max
if(PHP_INT_SIZE === 4){
if ($bytes > PHP_INT_MAX) {
$bytes = PHP_INT_MAX;
}
}
return $bytes;
} |
Let's keep this open, I would be happy to implement your suggestion as a fix :) |
Bug report
To replicate:
Run box.phar compile with 2G as PHP 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
The text was updated successfully, but these errors were encountered: