From 03218ffbd5faeda5e6a97f9109acebf7973ff385 Mon Sep 17 00:00:00 2001 From: Alexandre Daubois Date: Thu, 9 Dec 2021 17:15:29 +0100 Subject: [PATCH] [Config] In XmlUtils, avoid converting from octal every string starting with a 0 --- Tests/Util/XmlUtilsTest.php | 2 ++ Util/XmlUtils.php | 15 ++++++++++----- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/Tests/Util/XmlUtilsTest.php b/Tests/Util/XmlUtilsTest.php index a7a8ae980..9319b98ea 100644 --- a/Tests/Util/XmlUtilsTest.php +++ b/Tests/Util/XmlUtilsTest.php @@ -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'], diff --git a/Util/XmlUtils.php b/Util/XmlUtils.php index 41fb5a9e6..e03241494 100644 --- a/Util/XmlUtils.php +++ b/Util/XmlUtils.php @@ -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: @@ -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)); + } }