Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions ext/standard/tests/filters/user_filter_no_consume_leak.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
--TEST--
User filter that does not consume any input bucket leaks
--FILE--
<?php

class noop_filter extends php_user_filter {
public function filter($in, $out, &$consumed, bool $closing): int {
return PSFS_PASS_ON;
}
}

stream_filter_register("noop_filter", "noop_filter");

stream_filter_append(STDOUT, "noop_filter");

$out = fwrite(STDOUT, "Hello\n");
var_dump($out);

?>
--EXPECTF--
Warning: fwrite(): Unprocessed filter buckets remaining on input brigade in %s on line %d
int(0)
16 changes: 16 additions & 0 deletions ext/standard/user_filters.c
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,25 @@ php_stream_filter_status_t userfilter_filter(
}

if (buckets_in->head) {
php_stream_bucket *bucket;
do {
bucket = buckets_in->head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
} while (buckets_in->head);
php_error_docref(NULL, E_WARNING, "Unprocessed filter buckets remaining on input brigade");
}

/* Filter could've broken contract and added buckets anyway. */
if (ret == PSFS_FEED_ME && buckets_out->head) {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I'd need to double check whether this ret check is too specific (e.g. do we also need to do anything on FATAL).

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I just checked this and it seems fine with other return values.

php_stream_bucket *bucket;
do {
bucket = buckets_out->head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
} while (buckets_out->head);
}

/* filter resources are cleaned up by the stream destructor,
* keeping a reference to the stream resource here would prevent it
* from being destroyed properly */
Expand Down
7 changes: 0 additions & 7 deletions main/streams/filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -355,13 +355,6 @@ PHPAPI int php_stream_filter_append_ex(php_stream_filter_chain *chain, php_strea
Reset stream's internal read buffer since the filter is "holding" it. */
stream->readpos = 0;
stream->writepos = 0;

/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while (brig_out.head) {
bucket = brig_out.head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;
case PSFS_PASS_ON:
/* If any data is consumed, we cannot rely upon the existing read buffer,
Expand Down
14 changes: 1 addition & 13 deletions main/streams/streams.c
Original file line number Diff line number Diff line change
Expand Up @@ -631,12 +631,6 @@ PHPAPI zend_result _php_stream_fill_read_buffer(php_stream *stream, size_t size)
/* when a filter needs feeding, there is no brig_out to deal with.
* we simply continue the loop; if the caller needs more data,
* we will read again, otherwise out job is done here */

/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while ((bucket = brig_outp->head)) {
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;

case PSFS_ERR_FATAL:
Expand Down Expand Up @@ -1275,16 +1269,10 @@ static ssize_t _php_stream_write_filtered(php_stream *stream, const char *buf, s
/* some fatal error. Theoretically, the stream is borked, so all
* further writes should fail. */
consumed = (ssize_t) -1;
ZEND_FALLTHROUGH;
break;

case PSFS_FEED_ME:
/* need more data before we can push data through to the stream */
/* Filter could have added buckets anyway, but signalled that it did not return any. Discard them. */
while (brig_inp->head) {
bucket = brig_inp->head;
php_stream_bucket_unlink(bucket);
php_stream_bucket_delref(bucket);
}
break;
}

Expand Down
Loading