Skip to content

Commit 8cb74a2

Browse files
Merge branch '2.7' into 2.8
* 2.7: #20411 fix Yaml parsing for very long quoted strings [Doctrine Bridge] fix priority for doctrine event listeners Use PHP functions as array_map callbacks when possible [Validator] revert wrong Phpdoc change Use proper line endings
2 parents 841f675 + 329911d commit 8cb74a2

File tree

3 files changed

+77
-39
lines changed

3 files changed

+77
-39
lines changed

Inline.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,8 +149,8 @@ public static function dump($value, $exceptionOnInvalidType = false, $objectSupp
149149
case Escaper::requiresDoubleQuoting($value):
150150
return Escaper::escapeWithDoubleQuotes($value);
151151
case Escaper::requiresSingleQuoting($value):
152-
case preg_match(self::getHexRegex(), $value):
153-
case preg_match(self::getTimestampRegex(), $value):
152+
case Parser::preg_match(self::getHexRegex(), $value):
153+
case Parser::preg_match(self::getTimestampRegex(), $value):
154154
return Escaper::escapeWithSingleQuotes($value);
155155
default:
156156
return $value;
@@ -244,10 +244,10 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
244244
$i += strlen($output);
245245

246246
// remove comments
247-
if (preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
247+
if (Parser::preg_match('/[ \t]+#/', $output, $match, PREG_OFFSET_CAPTURE)) {
248248
$output = substr($output, 0, $match[0][1]);
249249
}
250-
} elseif (preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
250+
} elseif (Parser::preg_match('/^(.+?)('.implode('|', $delimiters).')/', substr($scalar, $i), $match)) {
251251
$output = $match[1];
252252
$i += strlen($output);
253253
} else {
@@ -282,7 +282,7 @@ public static function parseScalar($scalar, $delimiters = null, $stringDelimiter
282282
*/
283283
private static function parseQuotedScalar($scalar, &$i)
284284
{
285-
if (!preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
285+
if (!Parser::preg_match('/'.self::REGEX_QUOTED_STRING.'/Au', substr($scalar, $i), $match)) {
286286
throw new ParseException(sprintf('Malformed inline YAML string: %s.', substr($scalar, $i)));
287287
}
288288

@@ -530,16 +530,16 @@ private static function evaluateScalar($scalar, $references = array())
530530

531531
return '0' == $scalar[1] ? octdec($scalar) : (((string) $raw === (string) $cast) ? $cast : $raw);
532532
case is_numeric($scalar):
533-
case preg_match(self::getHexRegex(), $scalar):
533+
case Parser::preg_match(self::getHexRegex(), $scalar):
534534
return '0x' === $scalar[0].$scalar[1] ? hexdec($scalar) : (float) $scalar;
535535
case '.inf' === $scalarLower:
536536
case '.nan' === $scalarLower:
537537
return -log(0);
538538
case '-.inf' === $scalarLower:
539539
return log(0);
540-
case preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
540+
case Parser::preg_match('/^(-|\+)?[0-9,]+(\.[0-9]+)?$/', $scalar):
541541
return (float) str_replace(',', '', $scalar);
542-
case preg_match(self::getTimestampRegex(), $scalar):
542+
case Parser::preg_match(self::getTimestampRegex(), $scalar):
543543
$timeZone = date_default_timezone_get();
544544
date_default_timezone_set('UTC');
545545
$time = strtotime($scalar);

Parser.php

Lines changed: 57 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public function __construct($offset = 0, $totalNumberOfLines = null, array $skip
6161
*/
6262
public function parse($value, $exceptionOnInvalidType = false, $objectSupport = false, $objectForMap = false)
6363
{
64-
if (!preg_match('//u', $value)) {
64+
if (false === preg_match('//u', $value)) {
6565
throw new ParseException('The YAML value does not appear to be valid UTF-8.');
6666
}
6767
$this->currentLineNb = -1;
@@ -92,13 +92,13 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
9292
}
9393

9494
$isRef = $mergeNode = false;
95-
if (preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+?))?\s*$#u', $this->currentLine, $values)) {
95+
if (self::preg_match('#^\-((?P<leadspaces>\s+)(?P<value>.+))?$#u', rtrim($this->currentLine), $values)) {
9696
if ($context && 'mapping' == $context) {
9797
throw new ParseException('You cannot define a sequence item when in a mapping', $this->getRealCurrentLineNb() + 1, $this->currentLine);
9898
}
9999
$context = 'sequence';
100100

101-
if (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
101+
if (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
102102
$isRef = $matches['ref'];
103103
$values['value'] = $matches['value'];
104104
}
@@ -108,7 +108,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
108108
$data[] = $this->parseBlock($this->getRealCurrentLineNb() + 1, $this->getNextEmbedBlock(null, true), $exceptionOnInvalidType, $objectSupport, $objectForMap);
109109
} else {
110110
if (isset($values['leadspaces'])
111-
&& preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $values['value'], $matches)
111+
&& self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\{\[].*?) *\:(\s+(?P<value>.+))?$#u', rtrim($values['value']), $matches)
112112
) {
113113
// this is a compact notation element, add to next block and parse
114114
$block = $values['value'];
@@ -124,7 +124,10 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
124124
if ($isRef) {
125125
$this->refs[$isRef] = end($data);
126126
}
127-
} elseif (preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+?))?\s*$#u', $this->currentLine, $values) && (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))) {
127+
} elseif (
128+
self::preg_match('#^(?P<key>'.Inline::REGEX_QUOTED_STRING.'|[^ \'"\[\{].*?) *\:(\s+(?P<value>.+))?$#u', rtrim($this->currentLine), $values)
129+
&& (false === strpos($values['key'], ' #') || in_array($values['key'][0], array('"', "'")))
130+
) {
128131
if ($context && 'sequence' == $context) {
129132
throw new ParseException('You cannot define a mapping item when in a sequence', $this->currentLineNb + 1, $this->currentLine);
130133
}
@@ -191,7 +194,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
191194
$data += $parsed; // array union
192195
}
193196
}
194-
} elseif (isset($values['value']) && preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
197+
} elseif (isset($values['value']) && self::preg_match('#^&(?P<ref>[^ ]+) *(?P<value>.*)#u', $values['value'], $matches)) {
195198
$isRef = $matches['ref'];
196199
$values['value'] = $matches['value'];
197200
}
@@ -250,27 +253,7 @@ public function parse($value, $exceptionOnInvalidType = false, $objectSupport =
250253
return $value;
251254
}
252255

253-
switch (preg_last_error()) {
254-
case PREG_INTERNAL_ERROR:
255-
$error = 'Internal PCRE error.';
256-
break;
257-
case PREG_BACKTRACK_LIMIT_ERROR:
258-
$error = 'pcre.backtrack_limit reached.';
259-
break;
260-
case PREG_RECURSION_LIMIT_ERROR:
261-
$error = 'pcre.recursion_limit reached.';
262-
break;
263-
case PREG_BAD_UTF8_ERROR:
264-
$error = 'Malformed UTF-8 data.';
265-
break;
266-
case PREG_BAD_UTF8_OFFSET_ERROR:
267-
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
268-
break;
269-
default:
270-
$error = 'Unable to parse.';
271-
}
272-
273-
throw new ParseException($error, $this->getRealCurrentLineNb() + 1, $this->currentLine);
256+
throw new ParseException('Unable to parse', $this->getRealCurrentLineNb() + 1, $this->currentLine);
274257
}
275258
}
276259

@@ -515,7 +498,7 @@ private function parseValue($value, $exceptionOnInvalidType, $objectSupport, $ob
515498
return $this->refs[$value];
516499
}
517500

518-
if (preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
501+
if (self::preg_match('/^'.self::BLOCK_SCALAR_HEADER_PATTERN.'$/', $value, $matches)) {
519502
$modifiers = isset($matches['modifiers']) ? $matches['modifiers'] : '';
520503

521504
return $this->parseBlockScalar($matches['separator'], preg_replace('#\d+#', '', $modifiers), (int) abs($modifiers));
@@ -570,7 +553,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
570553

571554
// determine indentation if not specified
572555
if (0 === $indentation) {
573-
if (preg_match('/^ +/', $this->currentLine, $matches)) {
556+
if (self::preg_match('/^ +/', $this->currentLine, $matches)) {
574557
$indentation = strlen($matches[0]);
575558
}
576559
}
@@ -581,7 +564,7 @@ private function parseBlockScalar($style, $chomping = '', $indentation = 0)
581564
while (
582565
$notEOF && (
583566
$isCurrentLineBlank ||
584-
preg_match($pattern, $this->currentLine, $matches)
567+
self::preg_match($pattern, $this->currentLine, $matches)
585568
)
586569
) {
587570
if ($isCurrentLineBlank && strlen($this->currentLine) > $indentation) {
@@ -804,6 +787,49 @@ private function isStringUnIndentedCollectionItem()
804787
*/
805788
private function isBlockScalarHeader()
806789
{
807-
return (bool) preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
790+
return (bool) self::preg_match('~'.self::BLOCK_SCALAR_HEADER_PATTERN.'$~', $this->currentLine);
791+
}
792+
793+
/**
794+
* A local wrapper for `preg_match` which will throw a ParseException if there
795+
* is an internal error in the PCRE engine.
796+
*
797+
* This avoids us needing to check for "false" every time PCRE is used
798+
* in the YAML engine
799+
*
800+
* @throws ParseException on a PCRE internal error
801+
*
802+
* @see preg_last_error()
803+
*
804+
* @internal
805+
*/
806+
public static function preg_match($pattern, $subject, &$matches = null, $flags = 0, $offset = 0)
807+
{
808+
$ret = preg_match($pattern, $subject, $matches, $flags, $offset);
809+
if ($ret === false) {
810+
switch (preg_last_error()) {
811+
case PREG_INTERNAL_ERROR:
812+
$error = 'Internal PCRE error.';
813+
break;
814+
case PREG_BACKTRACK_LIMIT_ERROR:
815+
$error = 'pcre.backtrack_limit reached.';
816+
break;
817+
case PREG_RECURSION_LIMIT_ERROR:
818+
$error = 'pcre.recursion_limit reached.';
819+
break;
820+
case PREG_BAD_UTF8_ERROR:
821+
$error = 'Malformed UTF-8 data.';
822+
break;
823+
case PREG_BAD_UTF8_OFFSET_ERROR:
824+
$error = 'Offset doesn\'t correspond to the begin of a valid UTF-8 code point.';
825+
break;
826+
default:
827+
$error = 'Error.';
828+
}
829+
830+
throw new ParseException($error);
831+
}
832+
833+
return $ret;
808834
}
809835
}

Tests/ParserTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
class ParserTest extends TestCase
1919
{
20+
/** @var Parser */
2021
protected $parser;
2122

2223
protected function setUp()
@@ -1219,6 +1220,17 @@ public function parserThrowsExceptionWithCorrectLineNumberProvider()
12191220
),
12201221
);
12211222
}
1223+
1224+
public function testCanParseVeryLongValue()
1225+
{
1226+
$longStringWithSpaces = str_repeat('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx ', 20000);
1227+
$trickyVal = array('x' => $longStringWithSpaces);
1228+
1229+
$yamlString = Yaml::dump($trickyVal);
1230+
$arrayFromYaml = $this->parser->parse($yamlString);
1231+
1232+
$this->assertEquals($trickyVal, $arrayFromYaml);
1233+
}
12221234
}
12231235

12241236
class B

0 commit comments

Comments
 (0)