Skip to content

Commit

Permalink
bug #54113 [AssetMapper] Throw exception in Javascript compiler when …
Browse files Browse the repository at this point in the history
…PCRE error (smnandre)

This PR was squashed before being merged into the 6.4 branch.

Discussion
----------

[AssetMapper] Throw exception in Javascript compiler when PCRE error

| Q             | A
| ------------- | ---
| Branch?       | 6.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Issues        | Fix #...
| License       | MIT

`preg_match_callback` can return null when a PCRE error occured, leading there to a TypeError.

Let's throw an exception and expose the error message.

(follows #54078 / complementary to #54079 )

Commits
-------

2333b58 [AssetMapper] Throw exception in Javascript compiler when PCRE error
  • Loading branch information
fabpot committed Mar 4, 2024
2 parents a184a18 + 2333b58 commit 5f78910
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
Expand Up @@ -115,7 +115,7 @@ public function compile(string $content, MappedAsset $asset, AssetMapperInterfac
$relativeImportPath = $this->makeRelativeForJavaScript($relativeImportPath);

return str_replace($importedModule, $relativeImportPath, $fullImportString);
}, $content, -1, $count, \PREG_OFFSET_CAPTURE);
}, $content, -1, $count, \PREG_OFFSET_CAPTURE) ?? throw new RuntimeException(sprintf('Failed to compile JavaScript import paths in "%s". Error: "%s".', $asset->sourcePath, preg_last_error_msg()));
}

public function supports(MappedAsset $asset): bool
Expand Down
Expand Up @@ -641,4 +641,23 @@ public function testErrorMessageAvoidsCircularException()
// should not be caught.
$this->assertSame($content, $compiled);
}

public function testCompilerThrowsExceptionOnPcreError()
{
$compiler = new JavaScriptImportPathCompiler($this->createMock(ImportMapConfigReader::class));
$content = str_repeat('foo "import * ', 50);
$javascriptAsset = new MappedAsset('app.js', '/project/assets/app.js', publicPathWithoutDigest: '/assets/app.js');
$assetMapper = $this->createMock(AssetMapperInterface::class);

$this->expectException(RuntimeException::class);
$this->expectExceptionMessage('Failed to compile JavaScript import paths in "/project/assets/app.js". Error: "Backtrack limit exhausted".');

$limit = \ini_get('pcre.backtrack_limit');
ini_set('pcre.backtrack_limit', 10);
try {
$compiler->compile($content, $javascriptAsset, $assetMapper);
} finally {
ini_set('pcre.backtrack_limit', $limit);
}
}
}

0 comments on commit 5f78910

Please sign in to comment.