php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #22879 recursive function corrupt foreach loop
Submitted: 2003-03-25 10:14 UTC Modified: 2003-04-28 10:29 UTC
From: lev at centers dot ru Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 4.3.1 OS: FreeBSD 4.6
Private report: No CVE-ID: None
 [2003-03-25 10:14 UTC] lev at centers dot ru
<?
// Bag: recursive function corrupt foreach loop
// This bag is near bag #14607 but not equal
//
// ------- description of functions ----------------
// Incorrect function
  function out_node1($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) out_node1($i,$level+1);
    }
  }
// Correct function (bypass bag)
  function out_node($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) $node[]=$i;
    }
    for($i=0;$i<count($node);$i++) out_node($node[$i],$level+1);
  }
//------------------------------------------------------------
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=windows-1251">
</head>
<body>
<?
// ------- tree structure ----------------
// Tree:            number   parrent index
//    node1            0       -1
//        leaf1        1        0
//        leaf2        2        0
//        node2        3        0
//            leaf3    4        3
//            leaf4    5        3
// ------- body of script ----------------
  $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4');
  $ind=Array('-1','0','0','0','3','3');
//------------------------------------------------------------
echo "<BR><B>Incorrect example</B><BR>\n";
// print tree
  out_node1(0,0);
echo "<BR><B>Correct example</B><BR>\n";
// print tree
  out_node(0,0);
?>
</body>
</html>

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-03-25 17:01 UTC] helly@php.net
Your example is far to complex so i made it bogus. Try stripping it down and reopen the bug if you really think it is a PHP bug and not a bug in your script.

The only thing that came to my mind beeing able to cause
an error here is the fact you are using foreach in a nested
way. So i created a really short test for that and it works with both PHP 4.3.2 and PHP 5-dev.

[marcus@zaphod php4-HEAD]$ ../PHP_4_3_0/sapi/cli/php -r '$ar=array(0,1,2);foreach($ar as $n) foreach($ar as $m) echo "$n/$m\n";'
0/0
0/1
0/2
1/0
1/1
1/2
2/0
2/1
2/2
 [2003-03-26 06:01 UTC] lev at centers dot ru
"Nested foreach" and "recursive function call into foreach loop" is deferent things.
I guess: control variable of foreach loop is global and corrupted after recursive function call.

Compact variant of example:
--------------------------
<?
// Bag: recursive function corrupt foreach loop
  function out_node($k,$level)
  {
    global $name,$ind;
    for($i=0;$i<$level;$i++) echo "&nbsp;&nbsp;&nbsp;";
    echo "$name[$k]<BR>\n";
    foreach($ind as $i=>$index) {
      if($index==$k) out_node($i,$level+1);
    }
  }
?>
<html>
<head>
</head>
<body>
<?
  $name=Array('node1','leaf1','leaf2','node2','leaf3','leaf4');
  $ind=Array('-1','0','0','0','3','3');
  out_node(0,0);
?>
</body>
</html>

Result:
-------
node1
   leaf1

must be (Correct result):
------------------------
node1
   leaf1
   leaf2
   node2
      leaf3
      leaf4
 [2003-04-23 04:38 UTC] sniper@php.net
Just pass the $ind array as parameter always to the out_node1() and it works fine. Not bug.

 [2003-04-24 01:41 UTC] lev at centers dot ru
Yes, it works fine. But it is only bypass of bag
 [2003-04-24 04:27 UTC] sniper@php.net
It's not a bag, and it's not a bug either.

 [2003-04-24 10:11 UTC] lev at centers dot ru
Script works incorrect in case: 
   1 script has error
   2 bag in PHP.
If script has no error, then there is bag in PHP. I can?t see error in my script. So I think it is bag. 
Modification of script make situation where bag don?t appear. I call this ?bypass bag?.

As result:
If script works incorrect on deferent computers and anybody can?t find error in script ? it means BAG.
 [2003-04-28 10:29 UTC] sniper@php.net
It's _BUG_ if anything, I have no idea what 'BAG' in this context would be. :)

And it's _expected behaviour_, thus it's not a bug in PHP, or in your script. You just try to do something that you shouldn't do.

 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Sep 11 11:01:27 2024 UTC