Commit 92ba1e4ea0b for php.net
commit 92ba1e4ea0b7388c867e4d0cd348d2ed3f98e20a
Author: Ilija Tovilo <ilija.tovilo@me.com>
Date: Sun Feb 15 19:05:32 2026 +0100
Prevent closing of innerstream of php://temp stream
Fixes GH-21221
Closes GH-21222
diff --git a/NEWS b/NEWS
index ae0f6f3e8ff..1f084e98cb3 100644
--- a/NEWS
+++ b/NEWS
@@ -139,6 +139,8 @@ PHP NEWS
. Fixed bug GH-20370 (User stream filters could violate typed property
constraints). (alexandre-daubois)
. Allowed filtered streams to be casted as fd for select. (Jakub Zelenka)
+ . Fixed bug GH-21221 (Prevent closing of innerstream of php://temp stream).
+ (ilutov)
- Zip:
. Fixed ZipArchive callback being called after executor has shut down.
diff --git a/ext/standard/tests/gh21221.phpt b/ext/standard/tests/gh21221.phpt
new file mode 100644
index 00000000000..8435b480347
--- /dev/null
+++ b/ext/standard/tests/gh21221.phpt
@@ -0,0 +1,14 @@
+--TEST--
+GH-21221: Prevent closing of innerstream of php://temp stream
+--CREDITS--
+chongwick
+--FILE--
+<?php
+
+$f = fopen('php://temp', 'r+b');
+$resources = get_resources();
+fclose(end($resources));
+
+?>
+--EXPECTF--
+Warning: fclose(): cannot close the provided stream, as it must not be manually closed in %s on line %d
diff --git a/main/streams/streams.c b/main/streams/streams.c
index 85d2947c28a..32c7ba99f58 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -105,6 +105,7 @@ PHPAPI php_stream *php_stream_encloses(php_stream *enclosing, php_stream *enclos
php_stream *orig = enclosed->enclosing_stream;
php_stream_auto_cleanup(enclosed);
+ enclosed->flags |= PHP_STREAM_FLAG_NO_FCLOSE;
enclosed->enclosing_stream = enclosing;
return orig;
}