Skip to content

Commit

Permalink
[Config] In XmlUtils, avoid converting from octal every string starti…
Browse files Browse the repository at this point in the history
…ng with a 0
  • Loading branch information
alexandre-daubois committed Dec 12, 2021
1 parent 384b28e commit 03218ff
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions Tests/Util/XmlUtilsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,8 @@ public function getDataForPhpize(): array
[1, '1'],
[-1, '-1'],
[0777, '0777'],
[-511, '-0777'],
['0877', '0877'],
[255, '0xFF'],
[100.0, '1e2'],
[-120.0, '-1.2E2'],
Expand Down
15 changes: 10 additions & 5 deletions Util/XmlUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -236,15 +236,11 @@ public static function phpize($value)
case 'null' === $lowercaseValue:
return null;
case ctype_digit($value):
$raw = $value;
$cast = (int) $value;

return '0' == $value[0] ? octdec($value) : (($raw === (string) $cast) ? $cast : $raw);
case isset($value[1]) && '-' === $value[0] && ctype_digit(substr($value, 1)):
$raw = $value;
$cast = (int) $value;

return '0' == $value[1] ? octdec($value) : (($raw === (string) $cast) ? $cast : $raw);
return self::isOctal($value) ? \intval($value, 8) : (($raw === (string) $cast) ? $cast : $raw);
case 'true' === $lowercaseValue:
return true;
case 'false' === $lowercaseValue:
Expand Down Expand Up @@ -281,4 +277,13 @@ protected static function getXmlErrors($internalErrors)

return $errors;
}

private static function isOctal(string $str): bool
{
if ('-' === $str[0]) {
$str = substr($str, 1);
}

return $str === '0'.decoct(\intval($str, 8));
}
}

0 comments on commit 03218ff

Please sign in to comment.