php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52833 memory limit and custom error handler produces more errors than expected
Submitted: 2010-09-14 07:59 UTC Modified: 2010-09-14 12:03 UTC
From: me at unreal4u dot com Assigned:
Status: Not a bug Package: Output Control
PHP Version: 5.2.14 OS: CentOS5.5 32bits
Private report: No CVE-ID: None
 [2010-09-14 07:59 UTC] me at unreal4u dot com
Description:
------------
When I get to the memory limit of a script, a fatal error raises (expected behaviour). In order to nicely capture this error, I have created a custom error handler (through register_shutdown_function) which checks whether the script ended normally and so I can capture certain fatal errors. (Expected behaviour)
 
Inside this error handler, I raise the memory limit in order to capture this particular kind of error so that I can begin to check what happened. 
When I check with empty or isset if a certain variable which shouldn't be initialized yet exists (or doesn't evaluate to FALSE), the function returns a rather strange error instead of the default FALSE or TRUE. (Not expected).

Test script:
---------------
date_default_timezone_set('America/Santiago');
ini_set('memory_limit','1M');

function my_error_handler($errno = '0', $errstr = '[FATAL] General Error', $errfile = 'N/A', $errline = 'N/A', $errctx = '') {
  global $clean_exit;
  if(empty($clean_exit)) { // if isset or !empty, the script would have been exited cleanly.
    ini_set('memory_limit','16M'); // This would be just to have enough memory to print out the error.
    echo '<h5>-- BEGIN ERROR --</h5>Error N&deg;: <strong>'.$errno.'</strong><br />Error Description: <strong>'.$errstr.'</strong><br />File: <strong>'.$errfile.'</strong><br />Line: <strong>'.$errline.'</strong><br />Memory: <strong>'.round(memory_get_usage() / 1024).'</strong>KiB / <strong>'.round(memory_get_peak_usage() / 1024).'</strong>KiB';
    if (isset($errctx['r']['print'])) echo 'THIS LINE GIVES THE ERROR AND IS NOT PRINTED'; // isset or empty gives the same error
    echo '<h5>-- END ERROR --</h5>';
    die(); // shutdown_function == manual die() or else it will continue to execute.
  }
}
set_error_handler('my_error_handler');
register_shutdown_function('my_error_handler');

for ($i = 0; $i < 15000; $i++) $a[$i] = mt_rand(1,255);
$r['print'] = (string)$a[1]; // Just to fill something up
echo '<p>Everything fine.</p>';
$clean_exit = TRUE;

Expected result:
----------------
empty or isset returns FALSE, thus dying (normally) one level earlier.

Actual result:
--------------
An E_NOTICE appears: 
Uninitialized string offset: 0 on file X line Y


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-14 12:03 UTC] rquadling@php.net
-Status: Open +Status: Bogus
 [2010-09-14 12:03 UTC] rquadling@php.net
As reported on the mailing list http://news.php.net/php.general/308053.
 
PHP Copyright © 2001-2023 The PHP Group
All rights reserved.
Last updated: Sun Feb 05 18:03:43 2023 UTC