diff --git a/src/Composer/Autoload/ClassMapGenerator.php b/src/Composer/Autoload/ClassMapGenerator.php index abb0d5e9a..33c2d48c8 100644 --- a/src/Composer/Autoload/ClassMapGenerator.php +++ b/src/Composer/Autoload/ClassMapGenerator.php @@ -246,7 +246,7 @@ class ClassMapGenerator } // strip heredocs/nowdocs - $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*?)(?:\r\n|\n|\r)(?:\s*)\\2(?=\s+|[;,.)])}s', 'null', $contents); + $contents = preg_replace('{<<<[ \t]*([\'"]?)(\w+)\\1(?:\r\n|\n|\r)(?:.*(?=[\r\n]+[ \t]*\\2))[\r\n]+[ \t]*\\2(?=\s*[;,.)])}s', 'null', $contents); // strip strings $contents = preg_replace('{"[^"\\\\]*+(\\\\.[^"\\\\]*+)*+"|\'[^\'\\\\]*+(\\\\.[^\'\\\\]*+)*+\'}s', 'null', $contents); // strip leading non-php code if needed diff --git a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php index 1fb73c253..261fb7ced 100644 --- a/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php +++ b/tests/Composer/Test/Autoload/ClassMapGeneratorTest.php @@ -244,6 +244,25 @@ class ClassMapGeneratorTest extends TestCase $fs->removeDirectory($tempDir); } + public function testCreateMapDoesNotHitRegexBacktraceLimit() + { + $expected = array( + 'Foo\\StripNoise' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/StripNoise.php', + 'Foo\\VeryLongHeredoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongHeredoc.php', + 'Foo\\ClassAfterLongHereDoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongHeredoc.php', + 'Foo\\VeryLongPHP73Heredoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongPHP73Heredoc.php', + 'Foo\\VeryLongPHP73Nowdoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongPHP73Nowdoc.php', + 'Foo\\ClassAfterLongNowDoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongPHP73Nowdoc.php', + 'Foo\\VeryLongNowdoc' => realpath(__DIR__) . '/Fixtures/pcrebacktracelimit/VeryLongNowdoc.php', + ); + + ini_set('pcre.backtrack_limit', '30000'); + $result = ClassMapGenerator::createMap(__DIR__ . '/Fixtures/pcrebacktracelimit'); + ini_restore('pcre.backtrack_limit'); + + $this->assertEqualsNormalized($expected, $result); + } + protected function assertEqualsNormalized($expected, $actual, $message = '') { foreach ($expected as $ns => $path) { diff --git a/tests/Composer/Test/Autoload/Fixtures/classmap/StripNoise.php b/tests/Composer/Test/Autoload/Fixtures/classmap/StripNoise.php index 8944360ee..02da0a6c4 100644 --- a/tests/Composer/Test/Autoload/Fixtures/classmap/StripNoise.php +++ b/tests/Composer/Test/Autoload/Fixtures/classmap/StripNoise.php @@ -17,7 +17,12 @@ HEREDOC . <<< WHITESPACE class FailHeredocWhitespace { } -WHITESPACE . <<<"DOUBLEQUOTES" +WHITESPACE . <<< MARKERINTEXT + In PHP < 7.3, the docblock marker could occur in the text as long as it did not occur at the very start of the line. +But, what are you blind McFly, it's there. How else do you explain that wreck out there? Doc, Doc. Oh, no. You're alive. Bullet proof vest, how did you know, I never got a chance to tell you. About all that talk about screwing up future events, the space time continuum. Okay, alright, I'll prove it to you. + MARKERINTEXT + Look at my driver's license, expires 1987. Look at my birthday, for crying out load I haven't even been born yet. And, look at this picture, my brother, my sister, and me. Look at the sweatshirt, Doc, class of 1984. Why do you keep following me around? Hey beat it, spook, this don't concern you. +MARKERINTEXT . <<<"DOUBLEQUOTES" class FailHeredocDoubleQuotes { } diff --git a/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/StripNoise.php b/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/StripNoise.php new file mode 100644 index 000000000..8f93e8427 --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/StripNoise.php @@ -0,0 +1,87 @@ +'; + } + + public function test_simple_string() + { + return 'class FailSimpleString {}'; + } +} diff --git a/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/VeryLongHeredoc.php b/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/VeryLongHeredoc.php new file mode 100644 index 000000000..11f87546d --- /dev/null +++ b/tests/Composer/Test/Autoload/Fixtures/pcrebacktracelimit/VeryLongHeredoc.php @@ -0,0 +1,464 @@ +