Skip to content

Commit

Permalink
Allow to not close stream on rscr dtor in php cli sapi
Browse files Browse the repository at this point in the history
  • Loading branch information
bukka committed Jul 6, 2022
1 parent 6bd0175 commit 945bd53
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
3 changes: 3 additions & 0 deletions main/php_streams.h
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,9 @@ struct _php_stream_wrapper {
* Currently for internal use only. */
#define PHP_STREAM_FLAG_SUPPRESS_ERRORS 0x100

/* Do not close handle except it is explicitly closed by user (e.g. fclose) */
#define PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE 0x200

#define PHP_STREAM_FLAG_WAS_WRITTEN 0x80000000

struct _php_stream {
Expand Down
3 changes: 2 additions & 1 deletion main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -383,7 +383,8 @@ PHPAPI int _php_stream_free(php_stream *stream, int close_options) /* {{{ */

context = PHP_STREAM_CONTEXT(stream);

if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
if ((stream->flags & PHP_STREAM_FLAG_NO_CLOSE) ||
((stream->flags & PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE) && (close_options & PHP_STREAM_FREE_RSRC_DTOR))) {
preserve_handle = 1;
}

Expand Down
6 changes: 3 additions & 3 deletions sapi/cli/php_cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -540,9 +540,9 @@ static void cli_register_file_handles(void) /* {{{ */
* 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) s_in->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;

if (s_in==NULL || s_out==NULL || s_err==NULL) {
if (s_in) php_stream_close(s_in);
Expand Down
18 changes: 18 additions & 0 deletions sapi/cli/tests/gh8827.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
--TEST--
std handles can be deliberately closed
--SKIPIF--
<?php
if (php_sapi_name() != "cli") {
die("skip CLI only");
}
if (substr(PHP_OS, 0, 3) == 'WIN') {
die("skip not for Windows");
}
?>
--FILE--
<?php
fclose(STDERR);
var_dump(@fopen('php://stderr', 'a'));
?>
--EXPECT--
bool(false)

0 comments on commit 945bd53

Please sign in to comment.