Skip to content

Commit 704c17f

Browse files
authored
[TASK] Guard against infinite loop in parseList (#1427)
This can't be tested because there are currently no cases that would fail the test without the change. It is there as a preventative double-lock measure to make sure any future code changes do not introduce an infinite loop. It's a slight unoptimization, as it adds what should be a redundant runtime check, but at miniscule performance cost versus the value of preventing a waste of CPU power and memory in the case that something goes wrong.
1 parent f129f4c commit 704c17f

File tree

2 files changed

+10
-0
lines changed

2 files changed

+10
-0
lines changed

src/CSSList/CSSList.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,15 @@ public static function parseList(ParserState $parserState, CSSList $list): void
7272
$listItem = null;
7373
if ($usesLenientParsing) {
7474
try {
75+
$positionBeforeParse = $parserState->currentColumn();
7576
$listItem = self::parseListItem($parserState, $list);
7677
} catch (UnexpectedTokenException $e) {
7778
$listItem = false;
79+
// If the failed parsing did not consume anything that was to come ...
80+
if ($parserState->currentColumn() === $positionBeforeParse && !$parserState->isEnd()) {
81+
// ... the unexpected token needs to be skipped, otherwise there'll be an infinite loop.
82+
$parserState->consume(1);
83+
}
7884
}
7985
} else {
8086
$listItem = self::parseListItem($parserState, $list);

src/Parsing/ParserState.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,10 @@ public function parseCharacter(bool $isForIdentifier): ?string
192192
*
193193
* @throws UnexpectedEOFException
194194
* @throws UnexpectedTokenException
195+
*
196+
* @phpstan-impure
197+
* This method may change the state of the object by advancing the internal position;
198+
* it does not simply 'get' a value.
195199
*/
196200
public function consumeWhiteSpace(): array
197201
{

0 commit comments

Comments
 (0)