mirror of
https://github.com/phpbb/phpbb.git
synced 2025-06-08 04:18:52 +00:00
[ticket/10931] Make sure get_bytes() always returns either an int or a float.
PHPBB3-10931
This commit is contained in:
parent
e9348b172a
commit
09fb9a9efe
2 changed files with 39 additions and 9 deletions
|
@ -138,7 +138,7 @@ class phpbb_php_ini
|
||||||
if (is_numeric($value))
|
if (is_numeric($value))
|
||||||
{
|
{
|
||||||
// Already in bytes.
|
// Already in bytes.
|
||||||
return $value;
|
return $this->to_numeric($value);
|
||||||
}
|
}
|
||||||
else if (strlen($value) < 2)
|
else if (strlen($value) < 2)
|
||||||
{
|
{
|
||||||
|
@ -151,7 +151,7 @@ class phpbb_php_ini
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$value_numeric = (int) $value;
|
$value_numeric = $this->to_numeric($value);
|
||||||
|
|
||||||
switch ($value[strlen($value) - 1])
|
switch ($value[strlen($value) - 1])
|
||||||
{
|
{
|
||||||
|
@ -171,4 +171,17 @@ class phpbb_php_ini
|
||||||
|
|
||||||
return $value_numeric;
|
return $value_numeric;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Casts a numeric string $input to an appropriate numeric type (i.e. integer or float)
|
||||||
|
*
|
||||||
|
* @param string $input A numeric string.
|
||||||
|
*
|
||||||
|
* @return int|float Integer $input if $input fits integer,
|
||||||
|
* float $input otherwise.
|
||||||
|
*/
|
||||||
|
protected function to_numeric($input)
|
||||||
|
{
|
||||||
|
return ($input > PHP_INT_MAX) ? (float) $input : (int) $input;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -49,18 +49,35 @@ class phpbb_wrapper_phpbb_php_ini_test extends phpbb_test_case
|
||||||
$this->assertSame(false, $this->php_ini->get_float('phpBB'));
|
$this->assertSame(false, $this->php_ini->get_float('phpBB'));
|
||||||
}
|
}
|
||||||
|
|
||||||
public function test_get_bytes()
|
public function test_get_bytes_invalid()
|
||||||
{
|
{
|
||||||
$this->assertSame(false, $this->php_ini->get_bytes('phpBB'));
|
$this->assertSame(false, $this->php_ini->get_bytes('phpBB'));
|
||||||
$this->assertSame(false, $this->php_ini->get_bytes('k'));
|
$this->assertSame(false, $this->php_ini->get_bytes('k'));
|
||||||
$this->assertSame(false, $this->php_ini->get_bytes('-k'));
|
$this->assertSame(false, $this->php_ini->get_bytes('-k'));
|
||||||
$this->assertSame(false, $this->php_ini->get_bytes('M'));
|
$this->assertSame(false, $this->php_ini->get_bytes('M'));
|
||||||
$this->assertSame(false, $this->php_ini->get_bytes('-M'));
|
$this->assertSame(false, $this->php_ini->get_bytes('-M'));
|
||||||
$this->assertEquals(32 * pow(2, 20), $this->php_ini->get_bytes('32m'));
|
}
|
||||||
$this->assertEquals(- 32 * pow(2, 20), $this->php_ini->get_bytes('-32m'));
|
|
||||||
$this->assertEquals(8 * pow(2, 30), $this->php_ini->get_bytes('8G'));
|
/**
|
||||||
$this->assertEquals(- 8 * pow(2, 30), $this->php_ini->get_bytes('-8G'));
|
* @dataProvider get_bytes_data
|
||||||
$this->assertEquals(1234, $this->php_ini->get_bytes('1234'));
|
*/
|
||||||
$this->assertEquals(-12345, $this->php_ini->get_bytes('-12345'));
|
public function test_get_bytes($expected, $value)
|
||||||
|
{
|
||||||
|
$actual = $this->php_ini->get_bytes($value);
|
||||||
|
|
||||||
|
$this->assertTrue(is_float($actual) || is_int($actual));
|
||||||
|
$this->assertEquals($expected, $actual);
|
||||||
|
}
|
||||||
|
|
||||||
|
static public function get_bytes_data()
|
||||||
|
{
|
||||||
|
return array(
|
||||||
|
array(32 * pow(2, 20), '32m'),
|
||||||
|
array(- 32 * pow(2, 20), '-32m'),
|
||||||
|
array(8 * pow(2, 30), '8G'),
|
||||||
|
array(- 8 * pow(2, 30), '-8G'),
|
||||||
|
array(1234, '1234'),
|
||||||
|
array(-12345, '-12345'),
|
||||||
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue