Skip to content

Commit 1a4a22f

Browse files
committed
Merge branch 'PHP-8.4' into PHP-8.5
* PHP-8.4: Fix read buffer compaction in stream filter flush (#23439)
2 parents 9d5a615 + 79e4cd1 commit 1a4a22f

3 files changed

Lines changed: 34 additions & 2 deletions

File tree

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ PHP NEWS
6363
- Standard:
6464
. Fixed an out-of-bounds read when following a redirect response with an
6565
empty Location header. (iliaal)
66+
. Fixed read buffer compaction in php_stream_filter_flush(). (crystarm)
6667

6768
- Zip:
6869
. Fixed bug GH-17787 (ZipArchive stream stops reading early when the archive
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
--TEST--
2+
stream_filter_remove() compacts unread data before appending flushed data
3+
--FILE--
4+
<?php
5+
class ClosingSuffixFilter extends php_user_filter
6+
{
7+
public function filter($in, $out, &$consumed, $closing): int
8+
{
9+
while ($bucket = stream_bucket_make_writeable($in)) {
10+
$consumed += $bucket->datalen;
11+
stream_bucket_append($out, $bucket);
12+
}
13+
if ($closing) {
14+
stream_bucket_append($out, stream_bucket_new($this->stream, 'END'));
15+
}
16+
return PSFS_PASS_ON;
17+
}
18+
}
19+
stream_filter_register('closing-suffix', ClosingSuffixFilter::class);
20+
$stream = fopen('php://memory', 'w+');
21+
fwrite($stream, 'abcdef');
22+
rewind($stream);
23+
$filter = stream_filter_append($stream, 'closing-suffix', STREAM_FILTER_READ);
24+
var_dump(fread($stream, 2));
25+
var_dump(stream_filter_remove($filter));
26+
var_dump(stream_get_contents($stream));
27+
?>
28+
--EXPECT--
29+
string(2) "ab"
30+
bool(true)
31+
string(7) "cdefEND"

‎main/streams/filter.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -455,9 +455,9 @@ PHPAPI zend_result _php_stream_filter_flush(php_stream_filter *filter, bool fini
455455
/* Dump any newly flushed data to the read buffer */
456456
if (stream->readpos > 0) {
457457
/* Back the buffer up */
458-
memcpy(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
459-
stream->readpos = 0;
458+
memmove(stream->readbuf, stream->readbuf + stream->readpos, stream->writepos - stream->readpos);
460459
stream->writepos -= stream->readpos;
460+
stream->readpos = 0;
461461
}
462462
if (flushed_size > (stream->readbuflen - stream->writepos)) {
463463
/* Grow the buffer */

0 commit comments

Comments
 (0)