From de84d48c698af5b6765a1c4574f07ed782c7b854 Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Fri, 8 Jul 2022 14:24:34 +0200 Subject: [PATCH 1/2] Fix GH-8952: std streams can not be deliberately closed --- sapi/cli/php_cli.c | 14 ++++----- sapi/cli/tests/gh8827-001.phpt | 38 +++++++++++++++++++++++ sapi/cli/tests/gh8827-002.phpt | 47 ++++++++++++++++++++++++++++ sapi/cli/tests/gh8827-003.phpt | 57 ++++++++++++++++++++++++++++++++++ 4 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 sapi/cli/tests/gh8827-001.phpt create mode 100644 sapi/cli/tests/gh8827-002.phpt create mode 100644 sapi/cli/tests/gh8827-003.phpt diff --git a/sapi/cli/php_cli.c b/sapi/cli/php_cli.c index 47241f8af4e04..0ad53e813c944 100644 --- a/sapi/cli/php_cli.c +++ b/sapi/cli/php_cli.c @@ -538,14 +538,6 @@ 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 (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE; - if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE; - if (s_err) 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); @@ -553,6 +545,12 @@ static void cli_register_file_handles(bool no_close) /* {{{ */ 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); diff --git a/sapi/cli/tests/gh8827-001.phpt b/sapi/cli/tests/gh8827-001.phpt new file mode 100644 index 0000000000000..fab7b02d9e975 --- /dev/null +++ b/sapi/cli/tests/gh8827-001.phpt @@ -0,0 +1,38 @@ +--TEST-- +std handles can be deliberately closed 001 +--SKIPIF-- + +--FILE-- + +--EXPECT-- +STDIN: +bool(false) +STDERR: +bool(false) +STDOUT: diff --git a/sapi/cli/tests/gh8827-002.phpt b/sapi/cli/tests/gh8827-002.phpt new file mode 100644 index 0000000000000..d039896cb1cd9 --- /dev/null +++ b/sapi/cli/tests/gh8827-002.phpt @@ -0,0 +1,47 @@ +--TEST-- +std handles can be deliberately closed 002 +--SKIPIF-- + +--FILE-- + +--EXPECT-- +STDIN: +bool(false) +STDERR: +bool(false) +STDOUT: +bool(false) diff --git a/sapi/cli/tests/gh8827-003.phpt b/sapi/cli/tests/gh8827-003.phpt new file mode 100644 index 0000000000000..8c1fc49141f48 --- /dev/null +++ b/sapi/cli/tests/gh8827-003.phpt @@ -0,0 +1,57 @@ +--TEST-- +std handles can be deliberately closed 003 +--SKIPIF-- + +--FILE-- + +--EXPECT-- +stdoutFile: +Goes to stdoutFile +Also goes to stdoutFile +stderrFile: +Goes to stderrFile From 47d83d790908425a5e3d8431eec3883f95849abd Mon Sep 17 00:00:00 2001 From: Arnaud Le Blanc Date: Fri, 8 Jul 2022 15:06:17 +0200 Subject: [PATCH 2/2] rebuild