Skip to content

Commit 7c3e487

Browse files
committed
Fixed bug #65275
Make EG(exit_status) the single source of truth for the exit status, instead of having two variables that we cannot really keep synchronized.
1 parent 4c1a490 commit 7c3e487

File tree

5 files changed

+37
-26
lines changed

5 files changed

+37
-26
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ PHP NEWS
1717
(Nikita)
1818
. Fixed bug #79948 (Exit in auto-prepended file does not abort PHP execution).
1919
(Nikita)
20+
. Fixed bug #65275 (Calling exit() in a shutdown function does not change the
21+
exit value in CLI). (Nikita)
2022

2123
- Date:
2224
. Fixed bug #60302 (DateTime::createFromFormat should new static(), not new

main/main.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2471,7 +2471,6 @@ PHPAPI int php_execute_script(zend_file_handle *primary_file)
24712471
#endif
24722472
int retval = 0;
24732473

2474-
EG(exit_status) = 0;
24752474
#ifndef HAVE_BROKEN_GETCWD
24762475
# define OLD_CWD_SIZE 4096
24772476
old_cwd = do_alloca(OLD_CWD_SIZE, use_heap);

sapi/cli/php_cli.c

Lines changed: 16 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -607,7 +607,6 @@ static int do_cli(int argc, char **argv) /* {{{ */
607607
int behavior = PHP_MODE_STANDARD;
608608
char *reflection_what = NULL;
609609
volatile int request_started = 0;
610-
volatile int exit_status = 0;
611610
char *php_optarg = NULL, *orig_optarg = NULL;
612611
int php_optind = 1, orig_optind = 1;
613612
char *exec_direct=NULL, *exec_run=NULL, *exec_begin=NULL, *exec_end=NULL;
@@ -631,7 +630,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
631630
request_started = 1;
632631
php_print_info(0xFFFFFFFF);
633632
php_output_end_all();
634-
exit_status = (c == '?' && argc > 1 && !strchr(argv[1], c));
633+
EG(exit_status) = (c == '?' && argc > 1 && !strchr(argv[1], c));
635634
goto out;
636635

637636
case 'v': /* show php version & quit */
@@ -673,7 +672,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
673672
print_extensions();
674673
php_printf("\n");
675674
php_output_end_all();
676-
exit_status=0;
675+
EG(exit_status) = 0;
677676
goto out;
678677

679678
default:
@@ -848,7 +847,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
848847

849848
if (param_error) {
850849
PUTS(param_error);
851-
exit_status=1;
850+
EG(exit_status) = 1;
852851
goto err;
853852
}
854853

@@ -946,15 +945,14 @@ static int do_cli(int argc, char **argv) /* {{{ */
946945
}
947946

948947
if (interactive && cli_shell_callbacks.cli_shell_run) {
949-
exit_status = cli_shell_callbacks.cli_shell_run();
948+
EG(exit_status) = cli_shell_callbacks.cli_shell_run();
950949
} else {
951950
php_execute_script(&file_handle);
952-
exit_status = EG(exit_status);
953951
}
954952
break;
955953
case PHP_MODE_LINT:
956-
exit_status = php_lint_script(&file_handle);
957-
if (exit_status==SUCCESS) {
954+
EG(exit_status) = php_lint_script(&file_handle);
955+
if (EG(exit_status) == SUCCESS) {
958956
zend_printf("No syntax errors detected in %s\n", file_handle.filename);
959957
} else {
960958
zend_printf("Errors parsing %s\n", file_handle.filename);
@@ -980,7 +978,6 @@ static int do_cli(int argc, char **argv) /* {{{ */
980978
case PHP_MODE_CLI_DIRECT:
981979
cli_register_file_handles();
982980
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
983-
exit_status = EG(exit_status);
984981
break;
985982

986983
case PHP_MODE_PROCESS_STDIN:
@@ -991,10 +988,10 @@ static int do_cli(int argc, char **argv) /* {{{ */
991988

992989
cli_register_file_handles();
993990

994-
if (exec_begin && zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1) == FAILURE) {
995-
exit_status = EG(exit_status);
991+
if (exec_begin) {
992+
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);
996993
}
997-
while (exit_status == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
994+
while (EG(exit_status) == SUCCESS && (input=php_stream_gets(s_in_process, NULL, 0)) != NULL) {
998995
len = strlen(input);
999996
while (len > 0 && len-- && (input[len]=='\n' || input[len]=='\r')) {
1000997
input[len] = '\0';
@@ -1004,24 +1001,21 @@ static int do_cli(int argc, char **argv) /* {{{ */
10041001
ZVAL_LONG(&argi, ++index);
10051002
zend_hash_str_update(&EG(symbol_table), "argi", sizeof("argi")-1, &argi);
10061003
if (exec_run) {
1007-
if (zend_eval_string_ex(exec_run, NULL, "Command line run code", 1) == FAILURE) {
1008-
exit_status = EG(exit_status);
1009-
}
1004+
zend_eval_string_ex(exec_run, NULL, "Command line run code", 1);
10101005
} else {
10111006
if (script_file) {
10121007
if (cli_seek_file_begin(&file_handle, script_file) != SUCCESS) {
1013-
exit_status = 1;
1008+
EG(exit_status) = 1;
10141009
} else {
10151010
CG(skip_shebang) = 1;
10161011
php_execute_script(&file_handle);
1017-
exit_status = EG(exit_status);
10181012
}
10191013
}
10201014
}
10211015
efree(input);
10221016
}
1023-
if (exec_end && zend_eval_string_ex(exec_end, NULL, "Command line end code", 1) == FAILURE) {
1024-
exit_status = EG(exit_status);
1017+
if (exec_end) {
1018+
zend_eval_string_ex(exec_end, NULL, "Command line end code", 1);
10251019
}
10261020

10271021
break;
@@ -1091,7 +1085,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
10911085
display_ini_entries(NULL);
10921086
} else {
10931087
zend_printf("Extension '%s' not present.\n", reflection_what);
1094-
exit_status = 1;
1088+
EG(exit_status) = 1;
10951089
}
10961090
} else {
10971091
php_info_print_module(module);
@@ -1119,14 +1113,11 @@ static int do_cli(int argc, char **argv) /* {{{ */
11191113
if (translated_path) {
11201114
free(translated_path);
11211115
}
1122-
if (exit_status == 0) {
1123-
exit_status = EG(exit_status);
1124-
}
1125-
return exit_status;
1116+
return EG(exit_status);
11261117
err:
11271118
sapi_deactivate();
11281119
zend_ini_deactivate();
1129-
exit_status = 1;
1120+
EG(exit_status) = 1;
11301121
goto out;
11311122
}
11321123
/* }}} */

sapi/cli/tests/bug65275.inc

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
<?php
2+
3+
register_shutdown_function(function() {
4+
die(111);
5+
});
6+
7+
die(222);

sapi/cli/tests/bug65275.phpt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
--TEST--
2+
Bug #65275: Calling exit() in a shutdown function does not change the exit value in CLI
3+
--FILE--
4+
<?php
5+
6+
$php = getenv('TEST_PHP_EXECUTABLE');
7+
exec($php . ' ' . __DIR__ . '/bug65275.inc', $output, $exit_status);
8+
var_dump($exit_status);
9+
10+
?>
11+
--EXPECT--
12+
int(111)

0 commit comments

Comments
 (0)