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

Skip to content

Commit bb97f64

Browse files
committed
merged branch fabpot/bytes-conversion-epic-fail (PR #9048)
This PR was merged into the 2.3 branch. Discussion ---------- fixed bytes conversion when used on 32-bits systems | Q | A | ------------- | --- | Bug fix? | yes (on 32-bits systems) | New feature? | no | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | #8977 | License | MIT | Doc PR | n/a This PR reverts #7413 and #742, which does not work well when a number is big (3Go for instance) and the machine is 32bits. Commits ------- b3ae29d fixed bytes conversion when used on 32-bits systems
2 parents e6d983c + b3ae29d commit bb97f64

File tree

3 files changed

+46
-19
lines changed

3 files changed

+46
-19
lines changed

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,29 @@ class ServerParams
2323
*/
2424
public function getPostMaxSize()
2525
{
26-
$iniMax = $this->getNormalizedIniPostMaxSize();
26+
$iniMax = strtolower($this->getNormalizedIniPostMaxSize());
2727

2828
if ('' === $iniMax) {
2929
return null;
3030
}
3131

32-
if (preg_match('#^\+?(0X?)?(.*?)([KMG]?)$#', $iniMax, $match)) {
33-
$shifts = array('' => 0, 'K' => 10, 'M' => 20, 'G' => 30);
34-
$bases = array('' => 10, '0' => 8, '0X' => 16);
32+
$max = ltrim($iniMax, '+');
33+
if (0 === strpos($max, '0x')) {
34+
$max = intval($max, 16);
35+
} elseif (0 === strpos($max, '0')) {
36+
$max = intval($max, 8);
37+
} else {
38+
$max = intval($max);
39+
}
3540

36-
return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]];
41+
switch (substr($iniMax, -1)) {
42+
case 't': $max *= 1024;
43+
case 'g': $max *= 1024;
44+
case 'm': $max *= 1024;
45+
case 'k': $max *= 1024;
3746
}
3847

39-
return 0;
48+
return $max;
4049
}
4150

4251
/**

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -262,20 +262,29 @@ public function move($directory, $name = null)
262262
*/
263263
public static function getMaxFilesize()
264264
{
265-
$max = strtolower(ini_get('upload_max_filesize'));
265+
$iniMax = strtolower(ini_get('upload_max_filesize'));
266266

267-
if ('' === $max) {
267+
if ('' === $iniMax) {
268268
return PHP_INT_MAX;
269269
}
270270

271-
if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $max, $match)) {
272-
$shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
273-
$bases = array('' => 10, '0' => 8, '0x' => 16);
271+
$max = ltrim($iniMax, '+');
272+
if (0 === strpos($max, '0x')) {
273+
$max = intval($max, 16);
274+
} elseif (0 === strpos($max, '0')) {
275+
$max = intval($max, 8);
276+
} else {
277+
$max = intval($max);
278+
}
274279

275-
return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]];
280+
switch (substr($iniMax, -1)) {
281+
case 't': $max *= 1024;
282+
case 'g': $max *= 1024;
283+
case 'm': $max *= 1024;
284+
case 'k': $max *= 1024;
276285
}
277286

278-
return 0;
287+
return $max;
279288
}
280289

281290
/**

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

Lines changed: 15 additions & 6 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(strtolower(ini_get('memory_limit'))),
28+
'memory_limit' => $this->convertToBytes(ini_get('memory_limit')),
2929
);
3030
}
3131

@@ -79,13 +79,22 @@ private function convertToBytes($memoryLimit)
7979
return -1;
8080
}
8181

82-
if (preg_match('#^\+?(0x?)?(.*?)([kmg]?)$#', $memoryLimit, $match)) {
83-
$shifts = array('' => 0, 'k' => 10, 'm' => 20, 'g' => 30);
84-
$bases = array('' => 10, '0' => 8, '0x' => 16);
82+
$max = strtolower(ltrim($memoryLimit, '+'));
83+
if (0 === strpos($max, '0x')) {
84+
$max = intval($max, 16);
85+
} elseif (0 === strpos($max, '0')) {
86+
$max = intval($max, 8);
87+
} else {
88+
$max = intval($max);
89+
}
8590

86-
return intval($match[2], $bases[$match[1]]) << $shifts[$match[3]];
91+
switch (substr($memoryLimit, -1)) {
92+
case 't': $max *= 1024;
93+
case 'g': $max *= 1024;
94+
case 'm': $max *= 1024;
95+
case 'k': $max *= 1024;
8796
}
8897

89-
return 0;
98+
return $max;
9099
}
91100
}

0 commit comments

Comments
 (0)