bug #35332 [Yaml][Inline] Fail properly on empty object tag and empty 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 https://github.com/symfony/symfony/pull/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
-------

bdf02c0a7e [Yaml][Inline] Fail properly on empty object tag and empty const tag
This commit is contained in:
Nicolas Grekas 2020-02-03 11:17:31 +01:00
commit f758eca8b7
2 changed files with 57 additions and 1 deletions

View File

@ -506,7 +506,12 @@ class Inline
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)) {
@ -692,6 +697,10 @@ class Inline
return null;
case 0 === strpos($scalar, '!php/object'):
if (self::$objectSupport) {
if (!isset($scalar[12])) {
return false;
}
return unserialize(self::parseScalar(substr($scalar, 12)));
}
@ -717,6 +726,10 @@ class Inline
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);

View File

@ -799,4 +799,47 @@ class InlineTest extends TestCase
'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}'],
];
}
}