Commit 84ecdb9f044 for php.net
commit 84ecdb9f044275ee745ffbfda30c5f72117031d6
Author: Jakub Zelenka <bukka@php.net>
Date: Tue Sep 15 13:44:02 2026 +0200
ext/standard: Fix stream use after error handler in stream_get_meta_data
Fixes GH-23262
diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c
index d544e8bf5f0..a03aed1ccdd 100644
--- a/ext/standard/streamsfuncs.c
+++ b/ext/standard/streamsfuncs.c
@@ -563,7 +563,6 @@ PHP_FUNCTION(stream_get_meta_data)
add_assoc_bool(return_value, "blocked", 1);
add_assoc_bool(return_value, "eof", php_stream_eof(stream));
}
- php_stream_error_operation_end_for_stream(stream);
if (!Z_ISUNDEF(stream->wrapperdata)) {
Z_ADDREF_P(&stream->wrapperdata);
@@ -598,6 +597,7 @@ PHP_FUNCTION(stream_get_meta_data)
add_assoc_string(return_value, "uri", stream->orig_path);
}
+ php_stream_error_operation_end_for_stream(stream);
}
/* }}} */
diff --git a/ext/standard/tests/streams/gh23262.phpt b/ext/standard/tests/streams/gh23262.phpt
new file mode 100644
index 00000000000..3a1116b0cf9
--- /dev/null
+++ b/ext/standard/tests/streams/gh23262.phpt
@@ -0,0 +1,46 @@
+--TEST--
+GH-23262 (stream_get_meta_data() uses the stream after error_handler closes it)
+--FILE--
+<?php
+class InvalidEofStream
+{
+ public $context;
+
+ public function stream_open($path, $mode, $options, &$openedPath): bool
+ {
+ return true;
+ }
+
+ public function stream_eof()
+ {
+ return [];
+ }
+
+ public function stream_stat(): array
+ {
+ return [];
+ }
+}
+
+stream_wrapper_register('invalid-eof', InvalidEofStream::class);
+
+$stream = null;
+$context = stream_context_create([
+ 'stream' => [
+ 'error_mode' => StreamErrorMode::Silent,
+ 'error_handler' => static function (array $errors) use (&$stream): void {
+ echo "handler: {$errors[0]->code->name}\n";
+ fclose($stream);
+ },
+ ],
+]);
+
+$stream = fopen('invalid-eof://input', 'r', false, $context);
+$meta = stream_get_meta_data($stream);
+var_dump($meta['eof'], $meta['wrapper_type'], $meta['uri']);
+?>
+--EXPECT--
+handler: UserspaceInvalidReturn
+bool(true)
+string(10) "user-space"
+string(19) "invalid-eof://input"