Skip to content

Commit 945bd53

Browse files
committed
Allow to not close stream on rscr dtor in php cli sapi
1 parent 6bd0175 commit 945bd53

File tree

4 files changed

+26
-4
lines changed

4 files changed

+26
-4
lines changed

main/php_streams.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,9 @@ struct _php_stream_wrapper {
185185
* Currently for internal use only. */
186186
#define PHP_STREAM_FLAG_SUPPRESS_ERRORS 0x100
187187

188+
/* Do not close handle except it is explicitly closed by user (e.g. fclose) */
189+
#define PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE 0x200
190+
188191
#define PHP_STREAM_FLAG_WAS_WRITTEN 0x80000000
189192

190193
struct _php_stream {

main/streams/streams.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ PHPAPI int _php_stream_free(php_stream *stream, int close_options) /* {{{ */
383383

384384
context = PHP_STREAM_CONTEXT(stream);
385385

386-
if (stream->flags & PHP_STREAM_FLAG_NO_CLOSE) {
386+
if ((stream->flags & PHP_STREAM_FLAG_NO_CLOSE) ||
387+
((stream->flags & PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE) && (close_options & PHP_STREAM_FREE_RSRC_DTOR))) {
387388
preserve_handle = 1;
388389
}
389390

sapi/cli/php_cli.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -540,9 +540,9 @@ static void cli_register_file_handles(void) /* {{{ */
540540
* extensions which write to stderr or company during mshutdown/gshutdown
541541
* won't have the expected functionality.
542542
*/
543-
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_CLOSE;
544-
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_CLOSE;
545-
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_CLOSE;
543+
if (s_in) s_in->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
544+
if (s_out) s_out->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
545+
if (s_err) s_err->flags |= PHP_STREAM_FLAG_NO_RSCR_DTOR_CLOSE;
546546

547547
if (s_in==NULL || s_out==NULL || s_err==NULL) {
548548
if (s_in) php_stream_close(s_in);

sapi/cli/tests/gh8827.phpt

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
--TEST--
2+
std handles can be deliberately closed
3+
--SKIPIF--
4+
<?php
5+
if (php_sapi_name() != "cli") {
6+
die("skip CLI only");
7+
}
8+
if (substr(PHP_OS, 0, 3) == 'WIN') {
9+
die("skip not for Windows");
10+
}
11+
?>
12+
--FILE--
13+
<?php
14+
fclose(STDERR);
15+
var_dump(@fopen('php://stderr', 'a'));
16+
?>
17+
--EXPECT--
18+
bool(false)

0 commit comments

Comments
 (0)