From 18cddd6e556e2fb9e4c9a29eae2d5d87f2b18d68 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Sat, 6 Jul 2024 11:55:32 +0200 Subject: [PATCH] Prevent out-of-memory errors while regex array shape inference --- composer.json | 3 ++- composer.lock | 2 +- patches/Lexer.patch | 12 ++++++++++++ tests/PHPStan/Analyser/AnalyserIntegrationTest.php | 6 ++++++ tests/PHPStan/Analyser/data/bug-11292.php | 13 +++++++++++++ 5 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 patches/Lexer.patch create mode 100644 tests/PHPStan/Analyser/data/bug-11292.php diff --git a/composer.json b/composer.json index f7622977f9..4b2be0374e 100644 --- a/composer.json +++ b/composer.json @@ -91,7 +91,8 @@ ], "hoa/compiler": [ "patches/HoaException.patch", - "patches/Rule.patch" + "patches/Rule.patch", + "patches/Lexer.patch" ], "hoa/consistency": [ "patches/Consistency.patch" diff --git a/composer.lock b/composer.lock index 86b8aadc88..4924fa566c 100644 --- a/composer.lock +++ b/composer.lock @@ -4,7 +4,7 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "518cef83d930fabedc701ab1b60bebb4", + "content-hash": "00228dc36c9773581897d929dd4511ec", "packages": [ { "name": "clue/ndjson-react", diff --git a/patches/Lexer.patch b/patches/Lexer.patch new file mode 100644 index 0000000000..b2d84ed6e9 --- /dev/null +++ b/patches/Lexer.patch @@ -0,0 +1,12 @@ +diff --git a/Llk/Lexer.php b/Llk/Lexer.php +index 6851367..b8acf98 100644 +--- a/Llk/Lexer.php ++++ b/Llk/Lexer.php +@@ -281,7 +281,7 @@ class Lexer + $offset + ); + +- if (0 === $preg) { ++ if (0 === $preg || $preg === false) { + return null; + } diff --git a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php index 07f8def4f0..0b57adc405 100644 --- a/tests/PHPStan/Analyser/AnalyserIntegrationTest.php +++ b/tests/PHPStan/Analyser/AnalyserIntegrationTest.php @@ -1400,6 +1400,12 @@ public function testBug11283(): void $this->assertNoErrors($errors); } + public function testBug11292(): void + { + $errors = $this->runAnalyse(__DIR__ . '/data/bug-11292.php'); + $this->assertNoErrors($errors); + } + /** * @param string[]|null $allAnalysedFiles * @return Error[] diff --git a/tests/PHPStan/Analyser/data/bug-11292.php b/tests/PHPStan/Analyser/data/bug-11292.php new file mode 100644 index 0000000000..7d317e9e6c --- /dev/null +++ b/tests/PHPStan/Analyser/data/bug-11292.php @@ -0,0 +1,13 @@ +[\\x7f-\\xff]{1,64})(:[^]\\\\\\x00-\\x20\\"(),:-<>[\\x7f-\\xff]{1,64})?@)?((?:[-a-zA-Z0-9\\x7f-\\xff]{1,63}\\.)+[a-zA-Z\\x7f-\\xff][-a-zA-Z0-9\\x7f-\\xff]{1,62})((:[0-9]{1,5})?(/[!$-/0-9:;=@_~\':;!a-zA-Z\\x7f-\\xff]*?)?(\\?[!$-/0-9:;=@_\':;!a-zA-Z\\x7f-\\xff]+?)?(#[!$-/0-9?:;=@_\':;!a-zA-Z\\x7f-\\xff]+?)?)(?=[)\'?.!,;:]*(' . $nonUrl . '|$))}'; + if (preg_match($pattern, $s, $matches, PREG_OFFSET_CAPTURE, 0)) { + assertType('array}>', $matches); + } +};