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 = [];