-
Notifications
You must be signed in to change notification settings - Fork 7.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix #81243: Too much memory is allocated for preg_replace()
Trimming a potentially over-allocated string appears to be reasonable, so we drop the condition altogether. We also re-allocate twice the size needed in the first place, and not roughly tripple the size. Closes GH-7231.
- Loading branch information
Showing
3 changed files
with
36 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
--TEST-- | ||
Bug #81243 (Too much memory is allocated for preg_replace()) | ||
--FILE-- | ||
<?php | ||
$test_string = str_repeat('Eins zwei drei', 2000); | ||
|
||
$replaced = preg_replace('/\s/', '-', $test_string); | ||
$mem0 = memory_get_usage(); | ||
$replaced = str_repeat($replaced, 1); | ||
$mem1 = memory_get_usage(); | ||
var_dump($mem0 == $mem1); | ||
|
||
$replaced = preg_replace_callback('/\s/', function ($_) {return '-';}, $test_string); | ||
$mem0 = memory_get_usage(); | ||
$replaced = str_repeat($replaced, 1); | ||
$mem1 = memory_get_usage(); | ||
var_dump($mem0 == $mem1); | ||
?> | ||
--EXPECT-- | ||
bool(true) | ||
bool(true) |