8000 bug #57968 [Yaml] :bug: throw ParseException on invalid date (homersi… · symfony/symfony@d5b0f63 · GitHub
[go: up one dir, main page]

Skip to content

Commit d5b0f63

Browse files
committed
bug #57968 [Yaml] 🐛 throw ParseException on invalid date (homersimpsons)
This PR was merged into the 5.4 branch. Discussion ---------- [Yaml] 🐛 throw ParseException on invalid date | Q | A | ------------- | --- | Branch? | 5.4 <!-- see below --> | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | None <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT (found in symfony-tools/docs-builder#179) When parsing the following yaml: ``` date: 6418-75-51 ``` `symfony/yaml` will throw an exception: ``` $ php main.php PHP Fatal error: Uncaught Exception: Failed to parse time string (6418-75-51) at position 6 (5): Unexpected character in /tmp/symfony-yaml/vendor/symfony/yaml/Inline.php:714 Stack trace: #0 /tmp/symfony-yaml/vendor/symfony/yaml/Inline.php(714): DateTimeImmutable->__construct() #1 /tmp/symfony-yaml/vendor/symfony/yaml/Inline.php(312): Symfony\Component\Yaml\Inline::evaluateScalar() #2 /tmp/symfony-yaml/vendor/symfony/yaml/Inline.php(80): Symfony\Component\Yaml\Inline::parseScalar() #3 /tmp/symfony-yaml/vendor/symfony/yaml/Parser.php(790): Symfony\Component\Yaml\Inline::parse() #4 /tmp/symfony-yaml/vendor/symfony/yaml/Parser.php(341): Symfony\Component\Yaml\Parser->parseValue() #5 /tmp/symfony-yaml/vendor/symfony/yaml/Parser.php(86): Symfony\Component\Yaml\Parser->doParse() #6 /tmp/symfony-yaml/vendor/symfony/yaml/Yaml.php(77): Symfony\Component\Yaml\Parser->parse() #7 /tmp/symfony-yaml/main.php(8): Symfony\Component\Yaml\Yaml::parse() #8 {main} thrown in /tmp/symfony-yaml/vendor/symfony/yaml/Inline.php on line 714 ``` This is because the "month" is invalid. Fixing the "month" will trigger about the same issue because the "day" would be invalid. With the current change it will throw a `ParseException`. Commits ------- 6d71a7e 🐛 throw ParseException on invalid date
2 parents c582b76 + 6d71a7e commit d5b0f63

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

src/Symfony/Component/Yaml/Inline.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -700,8 +700,13 @@ private static function evaluateScalar(string $scalar, int $flags, array &$refer
700700
case Parser::preg_match('/^(-|\+)?[0-9][0-9_]*(\.[0-9_]+)?$/', $scalar):
701701
return (float) str_replace('_', '', $scalar);
702702
case Parser::preg_match(self::getTimestampRegex(), $scalar):
703-
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
704-
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
703+
try {
704+
// When no timezone is provided in the parsed date, YAML spec says we must assume UTC.
705+
$time = new \DateTime($scalar, new \DateTimeZone('UTC'));
706+
} catch (\Exception $e) {
707+
// Some dates accepted by the regex are not valid dates.
708+
throw new ParseException(\sprintf('The date "%s" could not be parsed as it is an invalid date.', $scalar), self::$parsedLineNumber + 1, $scalar, self::$parsedFilename, $e);
709+
}
705710

706711
if (Yaml::PARSE_DATETIME & $flags) {
707712
return $time;

src/Symfony/Component/Yaml/Tests/InlineTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,14 @@ public function testParseNestedTimestampListAsDateTimeObject(string $yaml, int $
579579
$this->assertEquals($expectedNested, Inline::parse($yamlNested, Yaml::PARSE_DATETIME));
580580
}
581581

582+
public function testParseInvalidDate()
583+
{
584+
$this->expectException(ParseException::class);
585+
$this->expectExceptionMessageMatches('/^The date "2024-50-50" could not be parsed as it is an invalid date.*/');
586+
587+
Inline::parse('2024-50-50', Yaml::PARSE_DATETIME);
588+
}
589+
582590
/**
583591
* @dataProvider getDateTimeDumpTests
584592
*/

0 commit comments

Comments
 (0)
0