php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Return to Bug #60131
Patch bug60131.diff revision 2011-10-30 03:14 UTC by cataphract@php.net

Patch bug60131.diff for Streams related Bug #60131

Patch version 2011-10-30 03:14 UTC

Return to Bug #60131 | Download this patch
Patch Revisions:

Developer: cataphract@php.net

Index: ext/standard/php_fopen_wrapper.c
===================================================================
--- ext/standard/php_fopen_wrapper.c	(revision 318567)
+++ ext/standard/php_fopen_wrapper.c	(working copy)
@@ -165,6 +165,7 @@
 	char *p, *token, *pathdup;
 	long max_memory;
 	FILE *file = NULL;
+	int force_leak = 0;
 
 	if (!strncasecmp(path, "php://", 6)) {
 		path += 6;
@@ -225,8 +226,13 @@
 			if (cli_in) {
 				fd = dup(fd);
 			} else {
+				/* make script inout and first stream synchronized by
+				 * making them use the same FILE* */
 				cli_in = 1;
 				file = stdin;
+				/* so that script input is not interrupted when the new stream
+				   is closed */
+				force_leak = 1;
 			}
 		} else {
 			fd = dup(STDIN_FILENO);
@@ -235,11 +241,12 @@
 		if (!strcmp(sapi_module.name, "cli")) {
 			static int cli_out = 0;
 			fd = STDOUT_FILENO;
-			if (cli_out++) {
+			if (cli_out) {
 				fd = dup(fd);
 			} else {
 				cli_out = 1;
 				file = stdout;
+				force_leak = 1;
 			}
 		} else {
 			fd = dup(STDOUT_FILENO);
@@ -248,11 +255,12 @@
 		if (!strcmp(sapi_module.name, "cli")) {
 			static int cli_err = 0;
 			fd = STDERR_FILENO;
-			if (cli_err++) {
+			if (cli_err) {
 				fd = dup(fd);
 			} else {
 				cli_err = 1;
 				file = stderr;
+				force_leak = 1;
 			}
 		} else {
 			fd = dup(STDERR_FILENO);
@@ -354,6 +362,11 @@
 
 	if (file) {
 		stream = php_stream_fopen_from_file(file, mode);
+		if (force_leak) {
+			/* force the stdlib resource only to be destroyed on shutdown */
+			zend_list_addref(stream->rsrc_id);
+			php_stream_auto_cleanup(stream);
+		}
 	} else {
 		stream = php_stream_fopen_from_fd(fd, mode, NULL);
 		if (stream == NULL) {
 
PHP Copyright © 2001-2020 The PHP Group
All rights reserved.
Last updated: Wed Sep 23 00:01:23 2020 UTC