php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #60455 stream_get_line reports two lines instead of one
Submitted: 2011-12-07 10:01 UTC Modified: 2012-09-01 16:15 UTC
From: jakub dot lopuszanski at nasza-klasa dot pl Assigned: cataphract (profile)
Status: Closed Package: Streams related
PHP Version: Irrelevant OS: linux
Private report: No CVE-ID: None
 [2011-12-07 10:01 UTC] jakub dot lopuszanski at nasza-klasa dot pl
Description:
------------
If a file consists of single line, and you use a straightforward loop over all 
lines using stream_get_line, you may be suprised that there are actually two 
lines, where the second one is empty string.

This is inconsistent with the case where you actually have two lines in a file, 
and stream_get_line reports two lines as expected without the additional empty 
third line.

This inconsistency makes it quite difficult to write a correct code.

Test script:
---------------
I have three files:

lopuszanski@vanisoft:~$ hexdump -C one_line.txt
00000000  61 0a                                             |a.|
00000002
lopuszanski@vanisoft:~$ hexdump -C two_lines.txt
00000000  61 0a 62 0a                                       |a.b.|
00000004
lopuszanski@vanisoft:~$ hexdump -C two_lines_one_of_which_is_empty.txt
00000000  61 0a 0a                                          |a..|
00000003

And the following script:
lopuszanski@vanisoft:~$ cat test.php
<?php
while(!feof(STDIN)){
  $line = stream_get_line(STDIN,1000000,"\n");
  var_dump($line);
}
?>


Expected result:
----------------
lopuszanski@vanisoft:~$ php test.php < one_line.txt
string(1) "a"
lopuszanski@vanisoft:~$ php test.php < two_lines.txt
string(1) "a"
string(1) "b"
lopuszanski@vanisoft:~$ php test.php < two_lines_one_of_which_is_empty.txt
string(1) "a"
string(0) ""


Actual result:
--------------
lopuszanski@vanisoft:~$ php test.php < one_line.txt
string(1) "a"
string(0) ""
lopuszanski@vanisoft:~$ php test.php < two_lines.txt
string(1) "a"
string(1) "b"
lopuszanski@vanisoft:~$ php test.php < two_lines_one_of_which_is_empty.txt
string(1) "a"
string(0) ""


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2011-12-11 21:07 UTC] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=320876
Log: - Fixed bug #60455: stream_get_line misbehaves if EOF is not detected together
  with the last read.
 [2011-12-11 21:08 UTC] cataphract@php.net
-Status: Open +Status: Closed -Assigned To: +Assigned To: cataphract
 [2011-12-11 21:08 UTC] cataphract@php.net
Fixed in SVN. Thanks.
 [2012-01-22 20:30 UTC] cataphract@php.net
Automatic comment from SVN on behalf of cataphract
Revision: http://svn.php.net/viewvc/?view=revision&amp;revision=322582
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected
  together with the last read).
- Fixed bug #60817 (stream_get_line() reads from stream even when there is
  already sufficient data buffered). stream_get_line() now behaves more like
  fgets(), as is documented.
#withheld commit to 5.4
 [2012-03-05 01:55 UTC] stas@php.net
-Status: Closed +Status: Re-Opened
 [2012-03-05 01:55 UTC] stas@php.net
Reopened to track 5.4. Please merge into 5.4 and close.
 [2012-04-18 09:46 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 [2012-04-18 09:46 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=faec3c51110baa0269124832274d138c48f86d0f
Log: - Fixed bug #60455: stream_get_line misbehaves if EOF is not detected together   with the last read.
 [2012-07-24 23:37 UTC] rasmus@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 [2012-07-24 23:38 UTC] rasmus@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=faec3c51110baa0269124832274d138c48f86d0f
Log: - Fixed bug #60455: stream_get_line misbehaves if EOF is not detected together   with the last read.
 [2012-09-01 16:14 UTC] cataphract@php.net
-Status: Re-Opened +Status: Open
 [2012-09-01 16:15 UTC] cataphract@php.net
-Status: Assigned +Status: Closed
 [2013-11-17 09:34 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=f5a457318217a66fddc0d47af04a00f144dcbcec
Log: - Further fix for bug #60455 (stream_get_line misbehaves if EOF is not detected   together with the last read). - Fixed bug #60817 (stream_get_line() reads from stream even when there is   already sufficient data buffered). stream_get_line() now behaves more like   fgets(), as is documented. #withheld commit to 5.4
 [2013-11-17 09:34 UTC] laruence@php.net
Automatic comment on behalf of cataphract
Revision: http://git.php.net/?p=php-src.git;a=commit;h=faec3c51110baa0269124832274d138c48f86d0f
Log: - Fixed bug #60455: stream_get_line misbehaves if EOF is not detected together   with the last read.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Tue Mar 19 04:01:31 2024 UTC