php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #78907 SplFileObject::fgetcsv() truncates data after CR char
Submitted: 2019-12-04 07:26 UTC Modified: 2019-12-04 08:16 UTC
Votes:2
Avg. Score:4.5 ± 0.5
Reproduced:2 of 2 (100.0%)
Same Version:1 (50.0%)
Same OS:0 (0.0%)
From: pandenitz at mail dot ru Assigned:
Status: Verified Package: SPL related
PHP Version: Irrelevant OS: Ubuntu 16.0.4
Private report: No CVE-ID: None
 [2019-12-04 07:26 UTC] pandenitz at mail dot ru
Description:
------------
Tested on PHP 7.1.26 - 7.4.0 

See https://3v4l.org/k1B5Y

The initial CSV file has CRLF line breaks.
4th row (starting from 0) has CR inside.

The parser truncates the chars after this CR and the line is incompletely parsed into an array.

Test script:
---------------
https://3v4l.org/k1B5Y

Expected result:
----------------
The row should be parsed correctly with an array element that contains CR inside because CRLF is the real linebreak.

Actual result:
--------------
The row is truncated after CR.

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2019-12-04 08:16 UTC] cmb@php.net
-Status: Open +Status: Verified
 [2019-12-04 08:16 UTC] cmb@php.net
Firstly, this CSV is invalid, since any linebreaks inside a field
need to be enclosed in $enclosure characters.  Also, you need to
set the READ_CSV flag.  But even if that is done[1], the line is
still skipped.  It works as expected with fgetcsv()[2], though.

[1] <https://3v4l.org/ga46S>
[2] <https://3v4l.org/Hg5TT>
 [2019-12-04 08:45 UTC] pandenitz at mail dot ru
See modified https://3v4l.org/UOIED with enclosures added, auto_detect_line_endings = 0 and "\0" escape (ntw why not "\\" ?).

The code has two variants for both SplFileObject::fgetcsv() and usual fgetcsv()

SplFileObject::fgetcsv() is still incorrect.

CSV can or can not have enclosures. It fails even with CR inside the enclosed column.
 [2019-12-04 09:44 UTC] pandenitz at mail dot ru
And SplFileObject::READ_CSV flag is not required for SplFileObject::fgetcsv()

It's required for SplFileObject::current() and SplFileObject::next(), or I am wrong?
 [2019-12-14 15:04 UTC] chrispmaiden at gmail dot com
Hi,

I'm not sure how relevant this is but I find that if I remove the SplFileObject::DROP_NEW_LINE flag from the code at https://3v4l.org/UOIED, I get a pass for both cases...

The docs about this flag at https://www.php.net/manual/en/class.splfileobject.php state: "https://www.php.net/manual/en/class.splfileobject.php" so perhaps that's what's happening with the SplFileObject case?

Best regards,

Chris
 [2019-12-14 15:29 UTC] chrispmaiden at gmail dot com
Sorry, I meant to paste what the docs say about the flag: "Drop newlines at the end of a line." so I guess that's what's happening?
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Wed Nov 13 02:01:31 2024 UTC