php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #44607 stream_get_line unable to correctly identify the "ending" in the stream content
Submitted: 2008-04-02 11:56 UTC Modified: 2008-11-03 23:38 UTC
Votes:4
Avg. Score:5.0 ± 0.0
Reproduced:4 of 4 (100.0%)
Same Version:4 (100.0%)
Same OS:3 (75.0%)
From: amoo_miki at yahoo dot com Assigned:
Status: Closed Package: Streams related
PHP Version: 5CVS, 6CVS (2008-08-19) OS: *
Private report: No CVE-ID:
 [2008-04-02 11:56 UTC] amoo_miki at yahoo dot com
Description:
------------
On some occasions stream_get_line acts weired by attempting the first seek without considering the "end of line" string.

Below is the sample code that shows the problem with a workaround to temporarily solve the issue (that might ring some bells for the person who plans on fixing this).

The code uses a small binary JPX file that can be fetched from http://barahmand.com/streambug/sample.txt

Reproduce code:
---------------
<?php 
$filename = 'sample.txt';
$arr = explode('<EndOfFrame>', file_get_contents($filename));
echo "Found in source at ".strlen($arr[0])."\n\n";
$fp = fopen($filename, "rb");
stream_get_line($fp, 15000, "<EndOfFrame>");
echo "Location in stream: ".ftell($fp)."\n";
echo "Rewinding...\n"; rewind($fp);
stream_get_line($fp, 15000, "<EndOfFrame>");
echo "Location in stream: ".ftell($fp)."\n";
rewind($fp);
echo "\nAttempting fix...\n";
stream_get_line($fp, 1, "<EndOfFrame>");
stream_get_line($fp, 15000, "<EndOfFrame>");
echo "Location in stream: ".ftell($fp)."\n";
echo "Rewinding...\n"; rewind($fp);
stream_get_line($fp, 15000, "<EndOfFrame>");
echo "Location in stream: ".ftell($fp)."\n";
fclose($fp);
?>

Expected result:
----------------
Found in source at 9295

Location in stream: 9307
Rewinding...
Location in stream: 9307

Attempting fix...
Location in stream: 9307
Rewinding...
Location in stream: 9307

Actual result:
--------------
Found in source at 9295

Location in stream: 15000
Rewinding...
Location in stream: 15000

Attempting fix...
Location in stream: 9307
Rewinding...
Location in stream: 9307

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-07-14 21:45 UTC] amoo_miki at yahoo dot com
Also tried with the latest CVS snapshot of 5.2.7-dev and it still gives incorrect results.
 [2008-07-14 21:54 UTC] amoo_miki at yahoo dot com
I should also add that the workaround (that works for this example file) does not always work.
 [2008-08-19 15:30 UTC] amoo_miki at yahoo dot com
I just got PHP 6.0.0-dev (built: Jul 28 2008 02:06:04) also configured and installed. that too has this problem of not working correctly.
 [2008-08-19 17:13 UTC] amoo_miki at yahoo dot com
Doing some more investigation:

if the "ending" is just a character, the function works fine and return the correct value, always.

stream_get_line in ./ext/standard/streamfuncs.c passes the action to php_stream_get_record in ./main/streams/streams.c where there is an explicit condition:
if delim_len == 1 the result is directly fetched using memchr() else php_memnstr() is requested to process the stream.
./main/php.h defines php_memnstr as zend_memnstr, and looking at ./Zend/zend_operatprs.h the function zend_memnstr uses memchr() and memcmp() and I should suspect something going wrong there.

The patch that sometimes forces the function to work correctly is a proof that all these functions work correct but there is a variable whose value is not being set correctly, ie not being initialized (maybe?)

Anyway, this function is a very useful one and it's really unfortunate that it has not been fixed in almost 5 months.
 [2008-11-03 23:38 UTC] lbarnaud@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Sun Apr 20 15:01:54 2014 UTC