From:             valyala at tut dot by
Operating system: any
PHP version:      4.3.7
PHP Bug Type:     Documentation problem
Bug description:  The example PHP code in docs http://php.net/gmp/ crashes PHP

Description:
------------
The following example code, which "will calculate factorial of 1000
(pretty big number) very fast" on documentation page http://php.net/gmp/
crashes PHP.
-------------------------------
<?php
function fact($x) 
{
    if ($x <= 1) {
        return 1;
    } else {
        return gmp_mul($x, fact($x-1));
    }
}

echo gmp_strval(fact(1000)) . "\n";
?> 
-------------------------------

The next PHP code avoids the crash:
-------------------------------
<?php
function fact($x) {
  $result = 1;
  while ($x > 1) {
    $result = gmp_mul($result, $x--);
  }
  return $result;
}

echo gmp_strval(fact(1000)) . "\n";
}
?>
-------------------------------

This bug related to the following bugs: #7720, #15522, #26212.
It seems that PHP4.3.7 (and older) don't handle stack overflow during
function calls.
You can find the maximum recursion depth for your version of PHP
independently. Just play with $rec_depth number in the following script:
---------------------------
<?php
function f($n) {
  if (--$n) f($n);
}

/* adjust this number to find the
   maximum recursion depth
*/
$rec_depth = 1000;
f($rec_depth);
?>
---------------------------

p.s. Are the volunteers in PHP developement team, who wanted to track
stack overflow problem?


Reproduce code:
---------------
see above

Expected result:
----------------
decimal view of 1000!

Actual result:
--------------
PHP crash

-- 
Edit bug report at http://bugs.php.net/?id=28827&edit=1
-- 
Try a CVS snapshot (php4):  http://bugs.php.net/fix.php?id=28827&r=trysnapshot4
Try a CVS snapshot (php5):  http://bugs.php.net/fix.php?id=28827&r=trysnapshot5
Fixed in CVS:               http://bugs.php.net/fix.php?id=28827&r=fixedcvs
Fixed in release:           http://bugs.php.net/fix.php?id=28827&r=alreadyfixed
Need backtrace:             http://bugs.php.net/fix.php?id=28827&r=needtrace
Need Reproduce Script:      http://bugs.php.net/fix.php?id=28827&r=needscript
Try newer version:          http://bugs.php.net/fix.php?id=28827&r=oldversion
Not developer issue:        http://bugs.php.net/fix.php?id=28827&r=support
Expected behavior:          http://bugs.php.net/fix.php?id=28827&r=notwrong
Not enough info:            http://bugs.php.net/fix.php?id=28827&r=notenoughinfo
Submitted twice:            http://bugs.php.net/fix.php?id=28827&r=submittedtwice
register_globals:           http://bugs.php.net/fix.php?id=28827&r=globals
PHP 3 support discontinued: http://bugs.php.net/fix.php?id=28827&r=php3
Daylight Savings:           http://bugs.php.net/fix.php?id=28827&r=dst
IIS Stability:              http://bugs.php.net/fix.php?id=28827&r=isapi
Install GNU Sed:            http://bugs.php.net/fix.php?id=28827&r=gnused
Floating point limitations: http://bugs.php.net/fix.php?id=28827&r=float

Reply via email to