From d7b528325345b36aaa9a21df3fe273e4b4fe8665 Mon Sep 17 00:00:00 2001 From: Jake Hotson Date: Thu, 20 Feb 2025 09:21:38 +0000 Subject: [PATCH] [CLEANUP] Ensure `ParserState::strsplit()` always returns array ... by throwing an exception on failure. --- src/Parsing/ParserState.php | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/Parsing/ParserState.php b/src/Parsing/ParserState.php index 44cceb29..5663e221 100644 --- a/src/Parsing/ParserState.php +++ b/src/Parsing/ParserState.php @@ -68,14 +68,14 @@ public function __construct($text, Settings $parserSettings, $lineNumber = 1) /** * Sets the charset to be used if the CSS does not contain an `@charset` declaration. + * + * @throws SourceException if the charset is UTF-8 and the content has invalid byte sequences */ public function setCharset(string $charset): void { $this->charset = $charset; $this->characters = $this->strsplit($this->text); - if (\is_array($this->characters)) { - $this->length = \count($this->characters); - } + $this->length = \count($this->characters); } /** @@ -466,12 +466,18 @@ private function strtolower($sString): string * @param string $sString * * @return array + * + * @throws SourceException if the charset is UTF-8 and the string contains invalid byte sequences */ private function strsplit($sString) { if ($this->parserSettings->bMultibyteSupport) { if ($this->streql($this->charset, 'utf-8')) { - return \preg_split('//u', $sString, -1, PREG_SPLIT_NO_EMPTY); + $result = \preg_split('//u', $sString, -1, PREG_SPLIT_NO_EMPTY); + if (!\is_array($result)) { + throw new SourceException('`preg_split` failed with error ' . \preg_last_error()); + } + return $result; } else { $length = \mb_strlen($sString, $this->charset); $result = [];