[php-src] Issue #15796: evade `exit_status` overwrite by `exit()`
| From: | determin1st | Date: | Sun, 08 Sep 2024 05:02:20 +0000 |
| Subject: | [php-src] Issue #15796: evade `exit_status` overwrite by `exit()` | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-247720@lists.php.net to get a copy of this message | ||
Issue: https://github.com/php/php-src/issues/15796
Author: determin1st
### Description
as you know there could be multiple
exit() occurrences with the help of
register_shutdown_function
my case exactly is a shutdown handler that executes once and does exit(), but original
exit_status is lost because exit overwrites it. i would like to keep the
original exit code. here's some code sample for quick showcase:
```php
register_shutdown_function(function() {
done(true);# bad termination
});
work();
done();# good termination
###
function work(): void
{
throw new \Exception('test');# same as exit(255);
#exit(1001);
}
function done(bool $bad=false): void
{
static $DID=0;
if ($DID) {
return;
}
$DID++;
# ...
# cleanup
# ...
exit($bad ? 1 : 0);
#exit(exit_status() ?: ($bad ? 1 : 0));# this would keep the original
}
```
to keep behavior the same, i propose to add exit_status() getter similar to
error_reporting() that only gets a exit_status value. what you think?
to test those, ive added it right after error_reporting
https://github.com/php/php-src/blob/e358634cdce6a7505b7d422c23ec205a483ad2fc/Zend/zend_builtin_functions.c#L457
```c
ZEND_FUNCTION(exit_status) // {{{
{
RETURN_LONG(EG(exit_status));
}
// }}}
```
but also have to these in https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions_arginfo.h
```c
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_exit_status, 0, 0, IS_LONG, 0)
ZEND_END_ARG_INFO()
// ...
ZEND_FUNCTION(exit_status);
// ...
ZEND_FE(error_reporting, arginfo_error_reporting)
```
it has some php file
(https://github.com/php/php-src/blob/master/Zend/zend_builtin_functions.stub.php) to generate, i
didnt get how to use it