|  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
View Add Comment Developer Edit
Welcome! If you don't have a Git account, you can't do anything here.
You can add a comment by following this link or if you reported this bug, you can edit this bug over here.
Block user comment
Status: Assign to:
Bug Type:
From: me at unreal4u dot com
New email:
PHP Version: OS:


 [2010-09-14 07:59 UTC] me at unreal4u dot com
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:

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.

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


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2010-09-14 12:03 UTC]
-Status: Open +Status: Bogus
 [2010-09-14 12:03 UTC]
As reported on the mailing list
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 12 22:01:29 2024 UTC