Skip to content

Commit 40b31fc

Browse files
committed
Fix #81302: Stream position after stream filter removed
When flushing the stream filters actually causes data to be written to the stream, we need to update its position, because that is not done by the streams' write methods. Closes GH-7354.
1 parent 79d564a commit 40b31fc

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ PHP NEWS
22
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
33
?? ??? ????, PHP 7.4.24
44

5+
- Core:
6+
. Fixed bug #81302 (Stream position after stream filter removed). (cmb)
57

68
26 Aug 2021, PHP 7.4.23
79

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
--TEST--
2+
Bug #81302 (Stream position after stream filter removed)
3+
--SKIPIF--
4+
<?php
5+
if (!extension_loaded('zlib')) die("skip zlib extension not available");
6+
?>
7+
--FILE--
8+
<?php
9+
$f = fopen("php://memory", "w+b");
10+
$z = stream_filter_append($f, "zlib.deflate", STREAM_FILTER_WRITE, 6);
11+
fwrite($f, "Testing");
12+
stream_filter_remove($z);
13+
$pos = ftell($f);
14+
fseek($f, 0);
15+
$count = strlen(fread($f, 1024));
16+
fclose($f);
17+
var_dump($count === $pos);
18+
?>
19+
--EXPECT--
20+
bool(true)

main/streams/filter.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,10 @@ PHPAPI int _php_stream_filter_flush(php_stream_filter *filter, int finish)
470470
} else if (chain == &(stream->writefilters)) {
471471
/* Send flushed data to the stream */
472472
while ((bucket = inp->head)) {
473-
stream->ops->write(stream, bucket->buf, bucket->buflen);
473+
ssize_t count = stream->ops->write(stream, bucket->buf, bucket->buflen);
474+
if (count > 0) {
475+
stream->position += count;
476+
}
474477
php_stream_bucket_unlink(bucket);
475478
php_stream_bucket_delref(bucket);
476479
}

0 commit comments

Comments
 (0)