php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #23327 Crash / Strange beahaviour when using file() and preg_match()
Submitted: 2003-04-24 08:35 UTC Modified: 2003-06-29 20:33 UTC
From: sysadmin at alexdupre dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.3.2-RC OS: FreeBSD 4-STABLE
Private report: No CVE-ID: None
 [2003-04-24 08:35 UTC] sysadmin at alexdupre dot com
Working script (using fopen/fgets/fclose):
<?php
if ($stats2 = fopen('http://194.183.89.2/uptime.php', 'r')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",fgets($stats2),$regs2);
        fclose($stats2);
}
if ($stats3 = fopen('http://194.183.89.3/uptime.php', 'r')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",fgets($stats3),$regs3);
        fclose($stats3);
}
if ($stats4 = fopen('http://194.183.89.4/uptime.php', 'r')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",fgets($stats4),$regs4);
        fclose($stats4);
}
echo "www2: $regs2[1], $regs2[2], $regs2[3]\n" .
     "www3: $regs3[1], $regs3[2], $regs3[3]\n" .
     "www4: $regs4[1], $regs4[2], $regs4[3]\n";
?>

$ php work.php
www2: 2.29, 2.73, 2.94
www3: 0.86, 0.77, 0.82
www4: 0.66, 0.72, 0.70

Not working script (using file):
<?php
if ($stats2 = file('http://194.183.89.2/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats2[0],$regs2);
}
if ($stats3 = file('http://194.183.89.3/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats3[0],$regs3);
}
if ($stats4 = file('http://194.183.89.4/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats4[0],$regs4);
}
echo "www2: $regs2[1], $regs2[2], $regs2[3]\n" .
     "www3: $regs3[1], $regs3[2], $regs3[3]\n" .
     "www4: $regs4[1], $regs4[2], $regs4[3]\n";
?>

$ php fail.php
www2: , ,
www3: T, T, P
www4: 0.38, 0.62, 0.66
php in free(): warning: chunk is already free
php in free(): warning: chunk is already free
php in free(): warning: chunk is already free
php in free(): warning: chunk is already free
php in free(): warning: chunk is already free
php in free(): warning: chunk is already free

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-04-24 13:41 UTC] sniper@php.net
Here's a bit better example which show exactly what happens:

<?php

if ($stats2 = file('http://194.183.89.2/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats2[0],$regs2);
        var_dump($regs2);
}

if ($stats3 = file('http://194.183.89.3/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats3[0],$regs3);
        var_dump($regs3);
}

if ($stats4 = file('http://194.183.89.4/uptime.php')) {
        preg_match("/averages?: ([0-9\.]+),[\s]+([0-9\.]+),[\s]+([0-9\.]+)/",$stats4[0],$regs4);
        var_dump($regs4);
}

var_dump($regs2);
var_dump($regs3);
var_dump($regs4);

?>

And output:
-----------

array(4) {
  [0]=>
  string(26) "averages: 2.80, 2.68, 2.46"
  [1]=>
  string(4) "2.80"
  [2]=>
  string(4) "2.68"
  [3]=>
  string(4) "2.46"
}
array(4) {
  [0]=>
  string(26) "averages: 0.62, 0.52, 0.46"
  [1]=>
  string(4) "0.62"
  [2]=>
  string(4) "0.52"
  [3]=>
  string(4) "0.46"
}
array(4) {
  [0]=>
  string(26) "averages: 1.10, 0.76, 0.74"
  [1]=>
  string(4) "1.10"
  [2]=>
  string(4) "0.76"
  [3]=>
  string(4) "0.74"
}
UNKNOWN:0
UNKNOWN:0
array(4) {
  [0]=>
  string(26) "averages: 1.10, 0.76, 0.74"
  [1]=>
  string(4) "1.10"
  [2]=>
  string(4) "0.76"
  [3]=>
  string(4) "0.74"
}

---

The last preg_match() call somehow mangles the $reg2 and $reg3 arrays??! (If I add more of these, always only the last $reg* array is okay)

This doesn't seem quite right. (No idea where the problem is, file() or preg_match())


 [2003-06-29 20:33 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, 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/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2026 The PHP Group
All rights reserved.
Last updated: Tue Feb 03 19:00:01 2026 UTC