Skip to content

Commit

Permalink
Stop closing stderr and stdout streams
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
morrisonlevi committed May 16, 2022
1 parent 33cd61c commit 751bfb8
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -538,19 +538,23 @@ static void cli_register_file_handles(bool no_close) /* {{{ */
s_out = php_stream_open_wrapper_ex("php://stdout", "wb", 0, NULL, sc_out);
s_err = php_stream_open_wrapper_ex("php://stderr", "wb", 0, NULL, sc_err);

/* Release stream resources, but don't free the underlying handles. Othewrise,
* extensions which write to stderr or company during mshutdown/gshutdown
* won't have the expected functionality.
*/
if (no_close) {
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
}

if (s_in==NULL || s_out==NULL || s_err==NULL) {
if (s_in) php_stream_close(s_in);
if (s_out) php_stream_close(s_out);
if (s_err) php_stream_close(s_err);
return;
}

if (no_close) {
s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
}

s_in_process = s_in;

php_stream_to_zval(s_in, &ic.value);
Expand Down Expand Up @@ -956,7 +960,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
switch (behavior) {
case PHP_MODE_STANDARD:
if (script_file) {
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);
}

if (interactive) {
Expand Down Expand Up @@ -991,7 +995,7 @@ static int do_cli(int argc, char **argv) /* {{{ */
}
break;
case PHP_MODE_CLI_DIRECT:
cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);
zend_eval_string_ex(exec_direct, NULL, "Command line code", 1);
break;

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

cli_register_file_handles(/* no_close */ PHP_DEBUG || num_repeats > 1);
cli_register_file_handles(/* no_close */ true);

if (exec_begin) {
zend_eval_string_ex(exec_begin, NULL, "Command line begin code", 1);
Expand Down

0 comments on commit 751bfb8

Please sign in to comment.