php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #27176 str_replace tries to use obscene amounts of ram
Submitted: 2004-02-07 06:29 UTC Modified: 2004-02-23 15:00 UTC
From: gaz at fission dot org dot uk Assigned: pollita (profile)
Status: Closed Package: Performance problem
PHP Version: 5.0.0b3 (beta3) OS: Slackware 9.1 Linux 2.6.1 kernel
Private report: No CVE-ID: None
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
(description)
Block user comment
Status: Assign to:
Package:
Bug Type:
Summary:
From: gaz at fission dot org dot uk
New email:
PHP Version: OS:

 

 [2004-02-07 06:29 UTC] gaz at fission dot org dot uk
Description:
------------
When str_replace is called with the 'mixed replace' value set to a string with a large (eg, 16,000) characters in it, it suddenly tries to asign an absolutly obscene amount of memory.

Reproduce code:
---------------
/* In the code that initially showed this up, $somefile was a 20kb html file, and $replacefile was a 6kb html file */

$input_text = file_get_contents($somefile);
$replace = file_get_contents($replacefile);
$match = "__RECENT__";

$output = str_replace($match,$replce,$input_text);

Actual result:
--------------
The actual code tries to allocate about 34MB of memory to do this str_replace. I think it keeps trying to reorder the char[] array, and so a large replacement string ties it up for a while. Changing to use explode() and foreach() tends to do the same thing but in no time, and without the huge memory tie-up.

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2004-02-07 14:01 UTC] sniper@php.net
Thank you for this bug report. To properly diagnose the problem, we
need a short but complete example script to be able to reproduce
this bug ourselves. 

A proper reproducing script starts with <?php and ends with ?>,
is max. 10-20 lines long and does not require any external 
resources such as databases, etc.

If possible, make the script source available online and provide
an URL to it here. Try avoid embedding huge scripts into the report.


 [2004-02-13 10:15 UTC] sniper@php.net
No feedback was provided. The bug is being suspended because
we assume that you are no longer experiencing the problem.
If this is not the case and you are able to provide the
information that was requested earlier, please do so and
change the status of the bug back to "Open". Thank you.

preg_replace() might be good alternative too..

 [2004-02-23 14:34 UTC] derick@php.net
<?php
ini_set("memory_limit", "12m");
$replacement = str_repeat("x", 12444);
$string = str_repeat("x", 9432);
$key =    "{BLURPS}";
                                                                                
$string = str_replace($key, $replacement, $string);
?>

 [2004-02-23 15:00 UTC] pollita@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 13:01:27 2024 UTC