php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #63240 stream_get_line return contains delimiter string
Submitted: 2012-10-08 16:03 UTC Modified: 2012-10-19 14:53 UTC
Votes:5
Avg. Score:4.4 ± 0.8
Reproduced:4 of 4 (100.0%)
Same Version:2 (50.0%)
Same OS:2 (50.0%)
From: scope at planetavent dot de Assigned: cataphract (profile)
Status: Closed Package: Streams related
PHP Version: 5.3.17 OS: Windows Server 2008 / RHEL 6.2
Private report: No CVE-ID: None
 [2012-10-08 16:03 UTC] scope at planetavent dot de
Description:
------------
On specific file input stream_get_line returns a "line" that actually contains the delimiter.

PHP 5.3.10 does not show this problem, PHP versions from 5.3.11 to 5.3.17 do. PHP 5.4.7 seems to be affected as well.

Bug #44607 seems to be related, but was fixed long time ago.

Test script:
---------------
<?php
$file = __DIR__ . '/input_dummy.txt';
$data = str_repeat( '.', 8184 ) . 'MM' . str_repeat( '.', 8190 ) . 'MM' . str_repeat( '.', 8128 ) . 'MM' . str_repeat( '.', 61 ) . 'MM' . str_repeat( '.', 20 );
file_put_contents( $file, $data );
$fh = fopen( $file, "rb" );
$delimiter = "MM";

while ( !feof( $fh ) )
{
    $before = ftell( $fh );
    $line = stream_get_line( $fh, 4096, $delimiter );
    $after = ftell( $fh );
    
    if ( strpos( $line, $delimiter  ) !== false )
    {
        echo "found delimiter in return! ($before / $after)\n$line";
		exit;
    }
}

echo "ok\n";

Expected result:
----------------
ok

Actual result:
--------------
found delimiter in return! (24508 / 24591)
.............................................................MM....................

Patches

getrecord-delimiter-search-fix (last revision 2012-10-09 09:02 UTC by datibbaw@php.net)

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2012-10-09 03:35 UTC] datibbaw@php.net
Reduced test:

  $file = __DIR__ . '/input_dummy.txt';
  $data = str_repeat( '.', 8189 ) . 'MMMM';
  file_put_contents( $file, $data );
  $fh = fopen( $file, "rb" );
  $delimiter = "MM";

  stream_get_line($fh, 4096, $delimiter);
  stream_get_line($fh, 4096, $delimiter);
  if ($delimiter === stream_get_line($fh, 4096, $delimiter)) {
    echo "BROKEN";
  } else {
    echo "OK";
  }
  fclose($fh);
  unlink($file);

The amount of dots seems to be related to the used buffer length:

(<number-of-dots> + 3) % (<buffer-length> x 2) == 0

The length of the delimiter also seems to play a role, but I'm not sure what 
that would be.
 [2012-10-09 09:02 UTC] datibbaw@php.net
The following patch has been added/updated:

Patch Name: getrecord-delimiter-search-fix
Revision:   1349773338
URL:        https://bugs.php.net/patch-display.php?bug=63240&patch=getrecord-delimiter-search-fix&revision=1349773338
 [2012-10-09 09:02 UTC] datibbaw@php.net
Added a patch that will apply to 5.4.4
 [2012-10-09 21:14 UTC] cataphract@php.net
-Assigned To: +Assigned To: cataphract
 [2012-10-09 21:14 UTC] cataphract@php.net
Thanks, I'll commit this tomorrow.

As to your other comment on the list about the bool(false) return instead of an empty string for an empty file, that was a bug fix. A return of "" would indicate an empty line. The result is now corect:

$ php -r '$fd = fopen("php://temp", "r+"); var_dump(stream_get_line($fd, 10, "MM"));'
bool(false)
$ php -r '$fd = fopen("php://temp", "r+"); fwrite($fd, "MM"); rewind($fd); var_dump(stream_get_line($fd, 10, "MM"), stream_get_line($fd, 10, "MM"));
string(0) ""
bool(false)
 [2012-10-12 18:23 UTC] cataphract@php.net
Automatic comment on behalf of glopes@nebm.ist.utl.pt
Revision: http://git.php.net/?p=php-src.git;a=commit;h=76601c4fd1052bd46e8db4addb1bb9dd3b001f98
Log: Fix bug #63240 on stream_get_line()
 [2012-10-12 18:23 UTC] cataphract@php.net
-Status: Assigned +Status: Closed
 [2012-10-19 12:52 UTC] datibbaw@php.net
Is it tomorrow yet? :)
 [2012-10-19 14:53 UTC] cataphract@php.net
Yes, this has already been committed. See the "All" tab in the "History" section of this bug report page and you'll find a link to the revision.
 [2014-10-07 23:22 UTC] stas@php.net
Automatic comment on behalf of glopes@nebm.ist.utl.pt
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=76601c4fd1052bd46e8db4addb1bb9dd3b001f98
Log: Fix bug #63240 on stream_get_line()
 [2014-10-07 23:32 UTC] stas@php.net
Automatic comment on behalf of glopes@nebm.ist.utl.pt
Revision: http://git.php.net/?p=php-src-security.git;a=commit;h=76601c4fd1052bd46e8db4addb1bb9dd3b001f98
Log: Fix bug #63240 on stream_get_line()
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 11:01:30 2024 UTC