php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #19103 print_r infinite recursion bug and probable code fix
Submitted: 2002-08-26 06:41 UTC Modified: 2002-08-28 05:50 UTC
From: mtf at maximasystems dot com Assigned:
Status: Closed Package: Scripting Engine problem
PHP Version: 4.2.2 OS: Linux
Private report: No CVE-ID: None
 [2002-08-26 06:41 UTC] mtf at maximasystems dot com
The infinite recursion bug in print_r still appears to be there as can be demonstrated by this little script:

<?php
    $tmp = array();
    $tmp2 = array();
    $tmp3 = array();
    $tmp[] =& $tmp2;
    $tmp[] =& $tmp3;
    $tmp2[] =& $tmp;
    $tmp2[] =& $tmp3;
    $tmp3[] =& $tmp;
    $tmp3[] =& $tmp2;
    print_r($tmp);
?>

I have looked into the code and I believe the bug lies in zend_print_zval_r_ex().

                        if (++expr->value.ht->nApplyCount>1) {
                                ZEND_PUTS(" *RECURSION*");
                                expr->value.ht->nApplyCount=0;
                                return;
                        }

seems to be wrong.  I believe it should read:

                        if (++expr->value.ht->nApplyCount>1) {
                                ZEND_PUTS(" *RECURSION*");
                                expr->value.ht->nApplyCount--;
                                return;
                        }

and the same change should be made lower down for the objects.  The current code spots the recursion because the counter is 1 and increased to 2, but then effectively hides that state from anything else by resetting the counter to 0.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2002-08-28 05:50 UTC] derick@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: Wed Oct 07 10:00:01 2026 UTC