Skip to content

Commit 751bfb8

Browse files
committed
Stop closing stderr and stdout streams
Extensions may (and do) write to stderr in mshutdown and similar. In the best case, with the stderr stream closed, it's just swallowed. However, some libraries will do things like try to detect color, and these will outright fail and cause an error path to be taken.
1 parent 33cd61c commit 751bfb8

File tree

1 file changed

+13
-9
lines changed

1 file changed

+13
-9
lines changed

sapi/cli/php_cli.c

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -538,19 +538,23 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
538538
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
539539
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);
540540

541+
/* Release stream resources, but don't free the underlying handles. Othewrise,
542+
* extensions which write to stderr or company during mshutdown/gshutdown
543+
* won't have the expected functionality.
544+
*/
545+
if (no_close) {
546+
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
547+
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
548+
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
549+
}
550+
541551
if (s_in==NULL || s_out==NULL || s_err==NULL) {
542552
if (s_in) php_stream_close(s_in);
543553
if (s_out) php_stream_close(s_out);
544554
if (s_err) php_stream_close(s_err);
545555
return;
546556
}
547557

548-
if (no_close) {
549-
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
550-
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
551-
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
552-
}
553-
554558
s_in_process = s_in;
555559

556560
php_stream_to_zval(s_in, &ic.value);
@@ -956,7 +960,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
956960
switch (behavior) {
957961
case PHP_MODE_STANDARD:
958962
if (script_file) {
959-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
963+
cli_register_file_handles(/* no_close */ true);
960964
}
961965

962966
if (interactive) {
@@ -991,7 +995,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
991995
}
992996
break;
993997
case PHP_MODE_CLI_DIRECT:
994-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
998+
cli_register_file_handles(/* no_close */ true);
995999
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
9961000
break;
9971001

@@ -1006,7 +1010,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
10061010
file_handle.filename = NULL;
10071011
}
10081012

1009-
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
1013+
cli_register_file_handles(/* no_close */ true);
10101014

10111015
if (exec_begin) {
10121016
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);

0 commit comments

Comments
 (0)