[php-src] PR #20058: Fix memory leak when user filter does not fully consume the input brigade buckets
| From: | nielsdos | Date: | Sat, 04 Oct 2025 10:53:33 +0000 |
| Subject: | [php-src] PR #20058: Fix memory leak when user filter does not fully consume the input brigade buckets | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-33478@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/20058
Author: nielsdos
Since the _php_stream_write_filtered() function assumes that the input brigade will be emptied (as
it clears one of the bucket brigades), any unconsumed bucket will leak. The other filters do not
suffer from this as they abort cleanly with an error code.
It's also possible to fix this in _php_stream_write_filtered() with an extra check, but since
the user filter is the only one that has this bug and already checks for this condition, we fix it
there instead.
For completeness, a fix in _php_stream_write_filtered() would look like this:
```diff
diff --git a/main/streams/streams.c b/main/streams/streams.c
index 372ed6635c3..720f3c15dd7 100644
--- a/main/streams/streams.c
+++ b/main/streams/streams.c
@@ -1242,6 +1242,15 @@ static ssize_t _php_stream_write_filtered(php_stream *stream, const char
*buf, s
if (status != PSFS_PASS_ON) {
break;
}
+ /* If the filter did not process the entire input brigade, then the buckets need to be freed
+ * manually or they will be lost when setting up the brigades for next iteration. */
+ if (UNEXPECTED(brig_inp->head)) {
+ do {
+ bucket = brig_inp->head;
+ php_stream_bucket_unlink(bucket);
+ php_stream_bucket_delref(bucket);
+ } while (brig_inp->head);
+ }
/* brig_out becomes brig_in.
* brig_in will always be empty here, as the filter MUST attach any un-consumed buckets
* to its own brigade */
```