Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 21291ca

Browse files
committed
improved bytes conversion method
1 parent 883bf90 commit 21291ca

File tree

4 files changed

+31
-16
lines changed

4 files changed

+31
-16
lines changed

src/Symfony/Component/Form/Extension/Validator/Util/ServerParams.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,14 @@ public function getPostMaxSize()
2929
return null;
3030
}
3131

32-
if (preg_match('#^(\d+)([bkmgt])#i', $iniMax, $match)) {
33-
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
34-
$iniMax = ($match[1] * (1 << $shift[strtolower($match[2])]));
32+
if (preg_match('#^\+?(0x?)?([^kmg]*)([KMG]?)#', $iniMax, $match)) {
33+
$shifts = array('' => 0, 'K' => 10, 'M' => 20, 'G' => 30);
34+
$bases = array('' => 10, '0' => 8, '0x' => 16);
35+
36+
return (intval($match[2], $bases[$match[1]]) * (1 << $shifts[$match[3]]));
3537
}
3638

37-
return (int) $iniMax;
39+
return 0;
3840
}
3941

4042
/**

src/Symfony/Component/HttpFoundation/File/UploadedFile.php

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -229,17 +229,19 @@ public function move($directory, $name = null)
229229
*/
230230
public static function getMaxFilesize()
231231
{
232-
$max = trim(ini_get('upload_max_filesize'));
232+
$max = strtolower(ini_get('upload_max_filesize'));
233233

234234
if ('' === $max) {
235235
return PHP_INT_MAX;
236236
}
237237

238-
if (preg_match('#^(\d+)([bkmgt])#i', $max, $match)) {
239-
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
240-
$max = ($match[1] * (1 << $shift[strtolower($match[2])]));
238+
if (preg_match('#^\+?(0x?)?([^kmg]*)([kmg]?)#', $max, $match)) {
239+
$shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
240+
$bases = array('' => 10, '0' => 8, '0x' => 16);
241+
242+
return (intval($match[2], $bases[$match[1]]) * (1 << $shifts[$match[3]]));
241243
}
242244

243-
return (integer) $max;
245+
return 0;
244246
}
245247
}

src/Symfony/Component/HttpKernel/DataCollector/MemoryDataCollector.php

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct()
2525
{
2626
$this->data = array(
2727
'memory' => 0,
28-
'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
28+
'memory_limit' => $this->convertToBytes(strtolower(ini_get('memory_limit'))),
2929
);
3030
}
3131

@@ -75,11 +75,17 @@ public function getName()
7575

7676
private function convertToBytes($memoryLimit)
7777
{
78-
if (preg_match('#^(\d+)([bkmgt])#i', $memoryLimit, $match)) {
79-
$shift = array('b' => 0, 'k' => 10, 'm' => 20, 'g' => 30, 't' => 40);
80-
$memoryLimit = ($match[1] * (1 << $shift[strtolower($match[2])]));
78+
if ('-1' === $memoryLimit) {
79+
return -1;
8180
}
8281

83-
return (int) $memoryLimit;
82+
if (preg_match('#^\+?(0x?)?([^kmg]*)([kmg]?)#', $memoryLimit, $match)) {
83+
$shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
84+
$bases = array('' => 10, '0' => 8, '0x' => 16);
85+
86+
return (intval($match[2], $bases[$match[1]]) * (1 << $shifts[$match[3]]));
87+
}
88+
89+
return 0;
8490
}
8591
}

src/Symfony/Component/HttpKernel/Tests/DataCollector/MemoryDataCollectorTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,14 @@ public function testBytesConversion($limit, $bytes)
4646
public function getBytesConversionTestData()
4747
{
4848
return array(
49-
array('-1', -1),
5049
array('2k', 2048),
51-
array('2K', 2048),
50+
array('2 k', 2048),
51+
array('+2 k', 2048),
52+
array('+2???k', 2048),
53+
array('0x10', 16),
54+
array('0xf', 15),
55+
array('010', 8),
56+
array('+0x10 k', 16 * 1024),
5257
array('1g', 1024 * 1024 * 1024),
5358
);
5459
}

0 commit comments

Comments
 (0)