Fix read buffer compaction in stream filter flush - #23439
Open
crystarm wants to merge 1 commit into
Open
Conversation
Contributor
Author
|
Small clarification: the allocated buffer size ( |
LamentXU123
requested changes
Aug 25, 2026
LamentXU123
left a comment
Member
There was a problem hiding this comment.
Could you add this test please?
--TEST--
stream_filter_remove() compacts unread data before appending flushed data
--FILE--
<?php
class ClosingSuffixFilter extends php_user_filter
{
public function filter($in, $out, &$consumed, $closing): int
{
while ($bucket = stream_bucket_make_writeable($in)) {
$consumed += $bucket->datalen;
stream_bucket_append($out, $bucket);
}
if ($closing) {
stream_bucket_append($out, stream_bucket_new($this->stream, 'END'));
}
return PSFS_PASS_ON;
}
}
stream_filter_register('closing-suffix', ClosingSuffixFilter::class);
$stream = fopen('php://memory', 'w+');
fwrite($stream, 'abcdef');
rewind($stream);
$filter = stream_filter_append($stream, 'closing-suffix', STREAM_FILTER_READ);
var_dump(fread($stream, 2));
var_dump(stream_filter_remove($filter));
var_dump(stream_get_contents($stream));
?>
--EXPECT--
string(2) "ab"
bool(true)
string(7) "cdefEND"Also, could you please rebase to 8.4 instead of master?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
php_stream_filter_flush()compacts unread data before appending buckets produced by a read filter.The source and destination ranges may overlap, making the use of
memcpy()undefined behavior. Additionally,readposwas reset before it was subtracted fromwritepos, so the buffer size was not adjusted and stale data could remain visible.Use
memmove()and adjustwriteposbefore resettingreadpos, matching the existing buffer compaction logic inphp_stream_fill_read_buffer().The issue was detected by static analysis: BUFFER_OVERLAP filter.c:[458:4].log