Skip to content

Commit 995442b

Browse files
authored
[BUGFIX] Use the safe regexp functions in ParserState (#1370)
Part of #1168
1 parent 1e3c2a0 commit 995442b

File tree

2 files changed

+12
-15
lines changed

2 files changed

+12
-15
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ Please also have a look at our
1818

1919
### Fixed
2020

21-
- Use typesafe versions of PHP functions (#1368)
21+
- Use typesafe versions of PHP functions (#1368, #1370)
2222

2323
### Documentation
2424

src/Parsing/ParserState.php

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@
77
use Sabberworm\CSS\Comment\Comment;
88
use Sabberworm\CSS\Settings;
99

10+
use function Safe\iconv;
11+
use function Safe\preg_match;
12+
use function Safe\preg_split;
13+
1014
/**
1115
* @internal since 8.7.0
1216
*/
@@ -63,8 +67,6 @@ public function __construct(string $text, Settings $parserSettings, int $lineNum
6367

6468
/**
6569
* Sets the charset to be used if the CSS does not contain an `@charset` declaration.
66-
*
67-
* @throws SourceException if the charset is UTF-8 and the content has invalid byte sequences
6870
*/
6971
public function setCharset(string $charset): void
7072
{
@@ -122,7 +124,7 @@ public function parseIdentifier(bool $ignoreCase = true): string
122124
}
123125
$character = null;
124126
while (!$this->isEnd() && ($character = $this->parseCharacter(true)) !== null) {
125-
if (\preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character)) {
127+
if (preg_match('/[a-zA-Z0-9\\x{00A0}-\\x{FFFF}_-]/Sux', $character) !== 0) {
126128
$result .= $character;
127129
} else {
128130
$result .= '\\' . $character;
@@ -146,13 +148,13 @@ public function parseCharacter(bool $isForIdentifier): ?string
146148
if ($this->comes('\\n') || $this->comes('\\r')) {
147149
return '';
148150
}
149-
if (\preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
151+
if (preg_match('/[0-9a-fA-F]/Su', $this->peek()) === 0) {
150152
return $this->consume(1);
151153
}
152154
$hexCodePoint = $this->consumeExpression('/^[0-9a-fA-F]{1,6}/u', 6);
153155
if ($this->strlen($hexCodePoint) < 6) {
154156
// Consume whitespace after incomplete unicode escape
155-
if (\preg_match('/\\s/isSu', $this->peek())) {
157+
if (preg_match('/\\s/isSu', $this->peek()) !== 0) {
156158
if ($this->comes('\\r\\n')) {
157159
$this->consume(2);
158160
} else {
@@ -166,7 +168,7 @@ public function parseCharacter(bool $isForIdentifier): ?string
166168
$utf32EncodedCharacter .= \chr($codePoint & 0xff);
167169
$codePoint = $codePoint >> 8;
168170
}
169-
return \iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
171+
return iconv('utf-32le', $this->charset, $utf32EncodedCharacter);
170172
}
171173
if ($isForIdentifier) {
172174
$peek = \ord($this->peek());
@@ -198,7 +200,7 @@ public function consumeWhiteSpace(): array
198200
{
199201
$comments = [];
200202
do {
201-
while (\preg_match('/\\s/isSu', $this->peek()) === 1) {
203+
while (preg_match('/\\s/isSu', $this->peek()) === 1) {
202204
$this->consume(1);
203205
}
204206
if ($this->parserSettings->usesLenientParsing()) {
@@ -291,7 +293,7 @@ public function consumeExpression(string $expression, ?int $maximumLength = null
291293
{
292294
$matches = null;
293295
$input = ($maximumLength !== null) ? $this->peek($maximumLength) : $this->inputLeft();
294-
if (\preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
296+
if (preg_match($expression, $input, $matches, PREG_OFFSET_CAPTURE) !== 1) {
295297
throw new UnexpectedTokenException($expression, $this->peek(5), 'expression', $this->lineNumber);
296298
}
297299

@@ -437,17 +439,12 @@ private function strtolower(string $string): string
437439

438440
/**
439441
* @return list<string>
440-
*
441-
* @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences
442442
*/
443443
private function strsplit(string $string): array
444444
{
445445
if ($this->parserSettings->hasMultibyteSupport()) {
446446
if ($this->streql($this->charset, 'utf-8')) {
447-
$result = \preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
448-
if (!\is_array($result)) {
449-
throw new SourceException('`preg_split` failed with error ' . \preg_last_error());
450-
}
447+
$result = preg_split('//u', $string, -1, PREG_SPLIT_NO_EMPTY);
451448
} else {
452449
$length = \mb_strlen($string, $this->charset);
453450
$result = [];

0 commit comments

Comments
 (0)