Fix GH-15796: Add the exit_status() function

This commit is contained in:
Alexandre Daubois 2025-08-11 10:22:45 +02:00
parent 2b5d978a85
commit 22a19ee7cb
No known key found for this signature in database
GPG key ID: DB5EB2A5B2556441
8 changed files with 68 additions and 1 deletions

2
NEWS
View file

@ -73,6 +73,8 @@ PHP NEWS
opened directory has been deprecated. (Girgias)
. Fixed bug GH-19153 (#[\Attribute] validation should error on
trait/interface/enum/abstract class). (DanielEScherzer)
. Added the exit_status() function to retrieve the current exit
status. (alexandre-daubois)
31 Jul 2025, PHP 8.5.0alpha4

View file

@ -593,6 +593,7 @@ PHP 8.5 UPGRADE NOTES
- Standard:
. Added array_first() and array_last().
RFC: https://wiki.php.net/rfc/array_first_last
. Added exit_status().
========================================
7. New Classes and Interfaces

View file

@ -0,0 +1,17 @@
--TEST--
exit_status() with uncaught exceptions setting exit code
--FILE--
<?php
register_shutdown_function(function() {
echo "Exit status is: " . exit_status() . "\n";
});
// set exit status to 255
throw new Exception("Test exception");
?>
--EXPECTF--
Fatal error: Uncaught Exception: Test exception in %s:%d
Stack trace:
#0 {main}
thrown in %s on line %d
Exit status is: 255

View file

@ -0,0 +1,20 @@
--TEST--
exit_status() in shutdown handler preserving original exit code
--FILE--
<?php
register_shutdown_function(function() {
echo "Current exit status: " . exit_status() . "\n";
$original_status = exit_status();
$final_status = $original_status ?: 255;
echo "Final status will be: " . $final_status . "\n";
});
echo "Setting exit status to 42\n";
exit(42);
?>
--EXPECT--
Setting exit status to 42
Current exit status: 42
Final status will be: 42

View file

@ -0,0 +1,14 @@
--TEST--
exit_status() with string exit codes
--FILE--
<?php
register_shutdown_function(function() {
echo "Exit status: " . exit_status() . "\n";
});
echo "Before exit with string\n";
exit("Error message");
?>
--EXPECT--
Before exit with string
Error messageExit status: 0

View file

@ -501,6 +501,13 @@ ZEND_FUNCTION(error_reporting)
}
/* }}} */
ZEND_FUNCTION(exit_status)
{
ZEND_PARSE_PARAMETERS_NONE();
RETURN_LONG(EG(exit_status));
}
static bool validate_constant_array_argument(HashTable *ht, int argument_number) /* {{{ */
{
bool ret = 1;

View file

@ -41,6 +41,8 @@ function strncasecmp(string $string1, string $string2, int $length): int {}
function error_reporting(?int $error_level = null): int {}
function exit_status(): int {}
function define(string $constant_name, mixed $value, bool $case_insensitive = false): bool {}
function defined(string $constant_name): bool {}

View file

@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
* Stub hash: 9b49f527064695c812cd204d9efc63c13681d942 */
* Stub hash: 754be326aa54bdac769a16875276cf0161491718 */
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_clone, 0, 1, IS_OBJECT, 0)
ZEND_ARG_TYPE_INFO(0, object, IS_OBJECT, 0)
@ -48,6 +48,8 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_error_reporting, 0, 0, IS_LONG,
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, error_level, IS_LONG, 1, "null")
ZEND_END_ARG_INFO()
#define arginfo_exit_status arginfo_func_num_args
ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_define, 0, 2, _IS_BOOL, 0)
ZEND_ARG_TYPE_INFO(0, constant_name, IS_STRING, 0)
ZEND_ARG_TYPE_INFO(0, value, IS_MIXED, 0)
@ -260,6 +262,7 @@ ZEND_FUNCTION(strncmp);
ZEND_FUNCTION(strcasecmp);
ZEND_FUNCTION(strncasecmp);
ZEND_FUNCTION(error_reporting);
ZEND_FUNCTION(exit_status);
ZEND_FUNCTION(define);
ZEND_FUNCTION(defined);
ZEND_FUNCTION(get_class);
@ -325,6 +328,7 @@ static const zend_function_entry ext_functions[] = {
ZEND_RAW_FENTRY("strcasecmp", zif_strcasecmp, arginfo_strcasecmp, ZEND_ACC_COMPILE_TIME_EVAL, NULL, NULL)
ZEND_RAW_FENTRY("strncasecmp", zif_strncasecmp, arginfo_strncasecmp, ZEND_ACC_COMPILE_TIME_EVAL, NULL, NULL)
ZEND_FE(error_reporting, arginfo_error_reporting)
ZEND_FE(exit_status, arginfo_exit_status)
ZEND_FE(define, arginfo_define)
ZEND_FE(defined, arginfo_defined)
ZEND_FE(get_class, arginfo_get_class)