Skip to content

Commit

Permalink
Prevent out-of-memory errors while regex array shape inference
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm authored Jul 6, 2024
1 parent 94a2fe8 commit 18cddd6
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 2 deletions.
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,8 @@
],
"hoa/compiler": [
"patches/HoaException.patch",
"patches/Rule.patch"
"patches/Rule.patch",
"patches/Lexer.patch"
],
"hoa/consistency": [
"patches/Consistency.patch"
Expand Down
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions patches/Lexer.patch
Original file line number Diff line number Diff line change
@@ -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;
}
6 changes: 6 additions & 0 deletions tests/PHPStan/Analyser/AnalyserIntegrationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down
13 changes: 13 additions & 0 deletions tests/PHPStan/Analyser/data/bug-11292.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php declare(strict_types = 1);

namespace Bug11292;

use function PHPStan\Testing\assertType;

function (string $s): void {
$nonUrl = "[^-_#$+.!*%'(),;/?:@~=&a-zA-Z0-9\x7f-\xff]";
$pattern = '{\\b(https?://)?(?:([^]\\\\\\x00-\\x20\\"(),:-<>[\\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<array{string, int<-1, max>}>', $matches);
}
};

0 comments on commit 18cddd6

Please sign in to comment.