|  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #76859 stream_get_line skips data if used with data-generating filter
Submitted: 2018-09-11 04:48 UTC Modified: -
From: tenzzor at gmail dot com Assigned:
Status: Closed Package: Streams related
PHP Version: master-Git-2018-09-11 (Git) OS: Linux
Private report: No CVE-ID: None
View Add Comment Developer Edit
Anyone can comment on a bug. Have a simpler test case? Does it work for you on a different platform? Let us know!
Just going to say 'Me too!'? Don't clutter the database with that please !
Your email address:
Solve the problem:
31 - 10 = ?
Subscribe to this entry?

 [2018-09-11 04:48 UTC] tenzzor at gmail dot com
This is happening with stream filters which create more data then they consume. For example, base64_encode, zlib.inflate.

The issue is happening because stream_get_line internally calls php_stream_get_record function, which repeatedly calls php_stream_fill_read_buffer until enough data is "accumulated" in the buffer. Problem with that is repeated calls to populate read buffer force reset state of stream->readpos and stream->writepos in case filters exist on the stream.
In other words, if on second call to get_record function, buffer holds some data, but does not have delimiter in it and length of data is less then required, then function attempts to put more data into buffer, which is in effect, resets buffer and content is lost.

Test script:

$h = fopen('php://temp', 'r+b');
$len = floor(1.5 * 8 * 1024); // 8*1024 == stream->chunk_size. We need a bit more.
$data = str_repeat('1', $len);
fwrite($h, $data);
stream_filter_append($h, 'convert.base64-encode');

$out = '';
while (!feof($h)) {
    $out .= stream_get_line($h, 1024);

echo $len, ' and ', strlen(base64_decode($out)), PHP_EOL;
assert($data == base64_decode($out));

Expected result:
12288 and 12288

Actual result:
12288 and 11520

Warning: assert(): assert($data == base64_decode($out)) failed in /usr/src/app/t3.php on line 17


Add a Patch

Pull Requests

Add a Pull Request


AllCommentsChangesGit/SVN commitsRelated reports
 [2019-10-03 04:52 UTC]
Automatic comment on behalf of
Log: Fix #76859 stream_get_line skips data if used with data-generating filter
 [2019-10-03 04:52 UTC]
-Status: Open +Status: Closed
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Feb 28 14:01:28 2024 UTC