facebook/hhvm — pull request #4143 by grahamf-idc (2014-11-05 23:03:15+00:00)
Title: Fix CRLF handling when they are split over input chunk buffers

This has been identified before in a couple of issues "readLine improperly handles crlf" (#1658) and "Respect auto_detect_line_endings ini setting" (#2897).

In both cases it was not a direct report of the problem (one moved it to HHVM Redis, the other to ```auto_detect_line_endings``` configuration). We have also run into this issue (using ```Predis```), but we also see it in general file handling due to inconsistent line counts between PHP/HHVM.

The problem stems from HHVM having a bug in file handling when reading CR/LF terminated lines, in this case when using ```fgets()```.

It appears this function chunks in 8K reads, looking for CR and/or LF combinations. However, there is an edge case where the 8K chunk can split the CR/LF character combination, such that it delivers the line ending in CR, then delivers the next line as the single LF. For us, this breaks ```Predis```, but also causes incorrect line counts.

To reproduce, create a file of "L\r\n" lines only using the following file writer code. The line length must be odd to cause the CR/LF to cross the HHVM read chunk boundary:

```PHP
fcrlf
[vantage@system hhvm]$ php fr.php