Bug #69181 [PATCH]: csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE
| From: | cmb@php.net | Date: | Thu, 28 Oct 2021 12:11:55 +0000 |
| Subject: | Bug #69181 [PATCH]: csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-237414@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=69181&edit=1
ID: 69181
Patch added by: cmb@php.net
Reported by: greg dot bowler at g105b dot com
Summary: csv parsing drops newlines within fields with
SplFileObject::DROP_NEW_LINE
Status: Verified
Type: Bug
Package: Filesystem function related
Operating System: Linux Ubuntu 14.04 LTS
PHP Version: 5.6.6
Assigned To: cmb
Block user comment: N
Private report: N
New Comment:
The following pull request has been associated:
Patch Name: Fix #69181: READ_CSV|DROP_NEW_LINE drops newlines within fields
On GitHub: https://github.com/php/php-src/pull/7618
Patch: https://github.com/php/php-src/pull/7618.patch
Previous Comments:
------------------------------------------------------------------------
[2019-07-03 14:59:24] stof at notk dot org
Any chance to get a fix for this ? I faced this bug today and it still affects current PHP versions.
See https://3v4l.org/MbRUS for the script I made.
------------------------------------------------------------------------
[2015-03-04 12:56:49] greg dot bowler at g105b dot com
The culprit of this bug seems to be the assumptive use of
strcspn on line 2055 of
spl_directory.c
http://lxr.php.net/xref/PHP_TRUNK/ext/spl/spl_directory.c#2055
size_t strcspn ( const char * str1, const char * str2 );
Scans str1 for the first occurrence of any of the characters that are part of str2, returning the
number of characters of str1 read before this first occurrence.
The key here is the word "any". It will look return the length up to the first \n in the
example above.
------------------------------------------------------------------------
[2015-03-04 11:47:03] greg dot bowler at g105b dot com
I am looking in ext/standard/file.c for where this behaviour could be changed, as I would like to
contribute a patch myself, but can't figure out where the flags are being read.
Can someone point me in the right direction, and I will look at submitting a patch?
------------------------------------------------------------------------
[2015-03-04 11:44:53] greg dot bowler at g105b dot com
Description:
------------
When using SplFileObject to parse a CSV file, unexpected behaviour occurs.
According to the documentation, the DROP_NEW_LINE flag drops newlines at the end of a line.
http://php.net/manual/en/class.splfileobject.php#splfileobject.constants.drop-new-line
Tests below show that not only the new-lines at the end of the file are dropped when using the flag,
but the first new line in any field is also dropped.
For example, the following raw csv:
col1,col2
testValue,one\ntwo\nthree
is interpreted like this when using DROP_NEW_LINES flag:
col1,col2
testValue,onetwo\nthree
Test script:
---------------
// Create a new CSV file using good old fputcsv:
$csvLines = [
["Name","Notes"],
["Person 1","This row has no new line"],
["Person 2","This row has\nthree\nnew\nlines"],
];
$filePath = "/tmp/data.csv";
$fp = fopen($filePath, "w");
foreach($csvLines as $line) {
fputcsv($fp, $line);
}
fclose($fp);
// Iteration 1, using DROP_NEW_LINE:
$file = new SplFileObject($filePath, "r");
$file->setFlags(
SplFileObject::READ_CSV |
SplFileObject::READ_AHEAD |
SplFileObject::SKIP_EMPTY |
SplFileObject::DROP_NEW_LINE
);
foreach($file as $i => $row) {
printf("%d \\n in %d ... " . PHP_EOL, substr_count($row[1], "\n"), $i);;
}
echo PHP_EOL . PHP_EOL;
// Iteration 2, with no DROP_NEW_LINE flag
$file = new SplFileObject($filePath, "r");
$file->setFlags(
SplFileObject::READ_CSV |
SplFileObject::READ_AHEAD |
SplFileObject::SKIP_EMPTY
);
foreach($file as $i => $row) {
printf("%d \\n in %d ... " . PHP_EOL, substr_count($row[1], "\n"), $i);;
}
Expected result:
----------------
Expected result is that the count of newlines in each iteration is the same.
There are no newlines at the end of the lines, so there should be none dropped.
Actual result:
--------------
Output of above test script:
0 \n in 0 ...
0 \n in 1 ...
2 \n in 2 ...
0 \n in 0 ...
0 \n in 1 ...
3 \n in 2 ...
Actual result is that the count of newlines is always 1 less than expected. The first \n character
within any field is stripped.
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=69181&edit=1