N°3806 Fix saying memory_limit isn't enough in CLI scripts and setup

This was caused by the strict comparison in \utils::IsMemoryLimitOk for the special "-1" value of memory_limit, which was added in c2f5cafa.
Fix was to change \utils::ConvertToBytes : it was returning original value when input was numeric (so if input is '-1' output was '-1')  now it always returns an int (input '-1' output -1)
This commit is contained in:
Pierre Goiffon
2021-06-28 14:35:34 +02:00
parent 8b9589744b
commit b5074c4cee
2 changed files with 10 additions and 6 deletions

View File

@@ -571,16 +571,16 @@ class utils
}
/**
* Helper function to convert a value expressed in a 'user friendly format'
* as in php.ini, e.g. 256k, 2M, 1G etc. Into a number of bytes
* @param mixed $value The value as read from php.ini (eg 256k, 2M, 1G etc.)
*
* @param mixed $value The value as read from php.ini
* @return int conversion to number of bytes
*
* @return number
* @since 2.7.5 3.0.0 convert to int numeric values
*
* @link https://www.php.net/manual/en/faq.using.php#faq.using.shorthandbytes Shorthand bytes value reference in PHP.net FAQ
*/
public static function ConvertToBytes($value)
{
$iReturn = $value;
if (!is_numeric($value)) {
$iLength = strlen($value);
$iReturn = substr($value, 0, $iLength - 1);
@@ -593,6 +593,8 @@ class utils
case 'K':
$iReturn *= 1024;
}
} else {
$iReturn = (int)$value;
}
return $iReturn;

View File

@@ -436,12 +436,14 @@ class UtilsTest extends \Combodo\iTop\Test\UnitTest\ItopTestCase
{
$iCurrentConvertedValue = utils::ConvertToBytes($sExpressionToConvert);
self::assertEquals($iExpectedConvertedValue, $iCurrentConvertedValue, 'Converted value wasn\'t the one expected !');
self::assertSame($iExpectedConvertedValue, $iCurrentConvertedValue, 'Value was converted but not of the expected type');
}
public function ConvertToBytesProvider()
{
return [
'123' => ['123', 123],
'123 int value' => ['123', 123],
'-1 no limit' => ['-1', -1],
'56k' => ['56k', 56 * 1024],
'512M' => ['512M', 512 * 1024 * 1024],
'2G' => ['2G', 2 * 1024 * 1024 * 1024],