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

Skip to content

Commit e8b7f0f

Browse files
committed
merged branch jfsimon/issue-7413 (PR #7456)
This PR was merged into the master branch. Discussion ---------- Improve bytes conversion method This PR improves bytes conversion `regex` method introduced in #7413 (thanks to @vicb's comments). * Adds support of `+` prefix. * Adds support of blank chars between `+`, number and unit. * Adds support of octal/hexa bases. Notice that this can not be unit tested for `ServerParams` and `UploadedFile` classes because `ini_set()` function does not work with `post_max_size` and `upload_max_filesize` settings. For information, this convertion is located in 3 classes: * `Symfony\Component\Form\Extension\Validator\Util\ServerParams` * `Symfony\Component\HttpFoundation\File\UploadedFile` * `Symfony\Component\HttpKernel\DataCollector\MemoryDataCollector` | Q | A | ------------- | --- | Bug fix? | yes | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #7413 Commits ------- 21291ca improved bytes conversion method
2 parents c1bd3b5 + 21291ca commit e8b7f0f

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
@@ -231,17 +231,19 @@ public function move($directory, $name = null)
231231
*/
232232
public static function getMaxFilesize()
233233
{
234-
$max = trim(ini_get('upload_max_filesize'));
234+
$max = strtolower(ini_get('upload_max_filesize'));
235235

236236
if ('' === $max) {
237237
return PHP_INT_MAX;
238238
}
239239

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

245-
return (integer) $max;
247+
return 0;
246248
}
247249
}

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)