[php-src] master: main/streams: Pair refused stream error operations with a no-op end

From: Date: Wed, 16 Sep 2026 20:10:02 +0000
Subject: [php-src] master: main/streams: Pair refused stream error operations with a no-op end
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-141238@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/39a230ccda052255249bf1ecda6d28f8daf6da2b Raw diff: https://github.com/php/php-src/commit/39a230ccda052255249bf1ecda6d28f8daf6da2b.diff main/streams: Pair refused stream error operations with a no-op end When the depth limit refuses a begin, the matching end popped an unrelated operation and desynchronized the stack. Changed paths: A ext/standard/tests/streams/stream_errors_operation_depth_limit.phpt M main/streams/php_stream_errors.h M main/streams/stream_errors.c Diff: diff --git a/ext/standard/tests/streams/stream_errors_operation_depth_limit.phpt b/ext/standard/tests/streams/stream_errors_operation_depth_limit.phpt new file mode 100644 index 000000000000..1eae9c5fd31d --- /dev/null +++ b/ext/standard/tests/streams/stream_errors_operation_depth_limit.phpt @@ -0,0 +1,72 @@ +--TEST-- +Stream errors: operations refused by the depth limit keep the stack consistent +--INI-- +zend.max_allowed_stack_size=-1 +--FILE-- +<?php +class RecursiveStream +{ + public $context; + public static int $depth = 0; + private int $reads = 0; + + public function stream_open($path, $mode, $options, &$openedPath): bool + { + return true; + } + + public function stream_read(int $count): string + { + $outermost = self::$depth === 0; + if (++self::$depth < 1010) { + $f = fopen('recursive-stream://x', 'r', false, $GLOBALS['ctx']); + fread($f, 1); + fclose($f); + } + self::$depth--; + if (!$outermost) { + return 'x'; + } + return ++$this->reads <= 2 ? str_repeat('A', $count + 1) : ''; + } + + public function stream_eof(): bool + { + return self::$depth > 0 || $this->reads >= 3; + } + + public function stream_stat(): array + { + return []; + } +} + +stream_wrapper_register('recursive-stream', RecursiveStream::class); + +$depthWarnings = 0; +set_error_handler(static function (int $severity, string $message) use (&$depthWarnings): bool { + if (str_contains($message, 'depth exceeded')) { + $depthWarnings++; + return true; + } + return false; +}); + +$ctx = stream_context_create(['stream' => [ + 'error_mode' => StreamErrorMode::Silent, + 'error_store' => StreamErrorStore::All, + 'error_handler' => static function (array $errors): void { + echo "handler: " . count($errors) . " error(s)\n"; + }, +]]); + +$stream = fopen('recursive-stream://x', 'r', false, $ctx); +var_dump(strlen(stream_get_contents($stream))); +fclose($stream); +var_dump($depthWarnings > 0, count(stream_last_errors())); +?> +--EXPECT-- +handler: 2 error(s) +int(16384) +bool(true) +int(2) diff --git a/main/streams/php_stream_errors.h b/main/streams/php_stream_errors.h index 304ca76a64c8..c49f518ea4d9 100644 --- a/main/streams/php_stream_errors.h +++ b/main/streams/php_stream_errors.h @@ -85,6 +85,7 @@ typedef struct { php_stream_error_operation *current_operation; uint32_t operation_depth; uint32_t operation_floor; + uint32_t refused_operations; php_stream_stored_error *stored_errors; uint32_t stored_count; php_stream_error_operation operation_pool[PHP_STREAM_ERROR_OPERATION_POOL_SIZE]; diff --git a/main/streams/stream_errors.c b/main/streams/stream_errors.c index 204fd4be4a56..e16d3425a4d3 100644 --- a/main/streams/stream_errors.c +++ b/main/streams/stream_errors.c @@ -232,6 +232,7 @@ PHPAPI void php_stream_error_state_cleanup(void) php_stream_error_state *state = &FG(stream_error_state); state->operation_floor = 0; + state->refused_operations = 0; while (state->current_operation) { php_stream_error_operation *op = state->current_operation; state->operation_depth--; @@ -301,6 +302,7 @@ PHPAPI php_stream_error_operation *php_stream_error_operation_begin(void) php_error_docref(NULL, E_WARNING, "Stream error operation depth exceeded (%"PRIu32"), possible infinite recursion", state->operation_depth); + state->refused_operations++; return NULL; } @@ -337,7 +339,10 @@ static void php_stream_error_add(zend_enum_StreamErrorCode code, const char *wra zend_string *message, const char *docref, int severity, bool terminating) { php_stream_error_operation *op = FG(stream_error_state).current_operation; - ZEND_ASSERT(op != NULL); + if (!op) { + zend_string_release(message); + return; + } php_stream_error_entry *entry = emalloc(sizeof(php_stream_error_entry)); entry->message = message; @@ -446,6 +451,11 @@ PHPAPI void php_stream_error_operation_end(const php_stream_context *context) php_stream_error_state *state = &FG(stream_error_state); php_stream_error_operation *op = state->current_operation; + if (state->refused_operations > 0) { + state->refused_operations--; + return; + } + if (!op) { return; } @@ -552,6 +562,11 @@ PHPAPI void php_stream_error_operation_end_for_stream(const php_stream *stream) php_stream_error_state *state = &FG(stream_error_state); php_stream_error_operation *op = state->current_operation; + if (state->refused_operations > 0) { + state->refused_operations--; + return; + } + if (!op) { return; } @@ -574,6 +589,11 @@ PHPAPI void php_stream_error_operation_abort(void) php_stream_error_state *state = &FG(stream_error_state); php_stream_error_operation *op = state->current_operation; + if (state->refused_operations > 0) { + state->refused_operations--; + return; + } + if (!op) { return; }

« previous php.cvs (#141238) next »