Skip to content

Commit

Permalink
bug #35332 [Yaml][Inline] Fail properly on empty object tag and empty…
Browse files Browse the repository at this point in the history
… const tag (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Yaml][Inline] Fail properly on empty object tag and empty const tag

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Rework of #35208 to not end up in `parseScalar` with an empty string or a boolean (and thus, avoid unfriendly error such as `Trying to access array offset on value of type bool`).

Ping @xabbuh

Commits
-------

bdf02c0 [Yaml][Inline] Fail properly on empty object tag and empty const tag
  • Loading branch information
nicolas-grekas committed Feb 3, 2020
2 parents 1f053f9 + bdf02c0 commit f758eca
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/Symfony/Component/Yaml/Inline.php
Expand Up @@ -506,7 +506,12 @@ private static function parseMapping($mapping, $flags, &$i = 0, $references = []

if ('!php/const' === $key) {
$key .= self::parseScalar($mapping, $flags, [':', ' '], $i, false, [], true);
$key = self::evaluateScalar($key, $flags);
if ('!php/const:' === $key && ':' !== $mapping[$i]) {
$key = '';
--$i;
} else {
$key = self::evaluateScalar($key, $flags);
}
}

if (':' !== $key && false === $i = strpos($mapping, ':', $i)) {
Expand Down Expand Up @@ -692,6 +697,10 @@ private static function evaluateScalar($scalar, $flags, $references = [])
return null;
case 0 === strpos($scalar, '!php/object'):
if (self::$objectSupport) {
if (!isset($scalar[12])) {
return false;
}

return unserialize(self::parseScalar(substr($scalar, 12)));
}

Expand All @@ -717,6 +726,10 @@ private static function evaluateScalar($scalar, $flags, $references = [])
return null;
case 0 === strpos($scalar, '!php/const'):
if (self::$constantSupport) {
if (!isset($scalar[11])) {
return '';
}

$i = 0;
if (\defined($const = self::parseScalar(substr($scalar, 11), 0, null, $i, false))) {
return \constant($const);
Expand Down
43 changes: 43 additions & 0 deletions src/Symfony/Component/Yaml/Tests/InlineTest.php
Expand Up @@ -799,4 +799,47 @@ public function getTestsForOctalNumbers()
'negative octal number' => [-28, '-034'],
];
}

/**
* @dataProvider phpObjectTagWithEmptyValueProvider
*/
public function testPhpObjectWithEmptyValue($expected, $value)
{
$this->assertSame($expected, Inline::parse($value, Yaml::PARSE_OBJECT));
}

public function phpObjectTagWithEmptyValueProvider()
{
return [
[false, '!php/object'],
[false, '!php/object '],
[false, '!php/object '],
[[false], '[!php/object]'],
[[false], '[!php/object ]'],
[[false, 'foo'], '[!php/object , foo]'],
];
}

/**
* @dataProvider phpConstTagWithEmptyValueProvider
*/
public function testPhpConstTagWithEmptyValue($expected, $value)
{
$this->assertSame($expected, Inline::parse($value, Yaml::PARSE_CONSTANT));
}

public function phpConstTagWithEmptyValueProvider()
{
return [
['', '!php/const'],
['', '!php/const '],
['', '!php/const '],
[[''], '[!php/const]'],
[[''], '[!php/const ]'],
[['', 'foo'], '[!php/const , foo]'],
[['' => 'foo'], '{!php/const: foo}'],
[['' => 'foo'], '{!php/const : foo}'],
[['' => 'foo', 'bar' => 'ccc'], '{!php/const : foo, bar: ccc}'],
];
}
}

0 comments on commit f758eca

Please sign in to comment.