[php-src] master: ext/standard: Fix stream use after error handler in stream_get_meta_data
| From: | Jakub Zelenka | Date: | Wed, 16 Sep 2026 20:09:45 +0000 |
| Subject: | [php-src] master: ext/standard: Fix stream use after error handler in stream_get_meta_data | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-141235@lists.php.net to get a copy of this message | ||
Author: Jakub Zelenka (bukka)
Date: 2026-09-16T22:09:39+02:00
Commit: https://github.com/php/php-src/commit/84ecdb9f044275ee745ffbfda30c5f72117031d6
Raw diff: https://github.com/php/php-src/commit/84ecdb9f044275ee745ffbfda30c5f72117031d6.diff
ext/standard: Fix stream use after error handler in stream_get_meta_data
Fixes GH-23262
Changed paths:
A ext/standard/tests/streams/gh23262.phpt
M ext/standard/streamsfuncs.c
Diff:
diff --git a/ext/standard/streamsfuncs.c b/ext/standard/streamsfuncs.c
index d544e8bf5f0f..a03aed1ccdd4 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 000000000000..3a1116b0cf9a
--- /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"