Skip to content

Fixes bug #69181 (csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE) #1148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,7 @@ PHP NEWS
statements option). (peter dot wolanin at acquia dot com)

- SPL:
. Fixed bug #69181 (csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE). (greg dot bowler at g105b dot com)
. Fixed bug #66405 (RecursiveDirectoryIterator::CURRENT_AS_PATHNAME
breaks the RecursiveIterator). (Paul Garvin)
. Fixed bug #65213 (cannot cast SplFileInfo to boolean) (Tjerk)
Expand Down
3 changes: 2 additions & 1 deletion ext/spl/spl_directory.c
Original file line number Diff line number Diff line change
Expand Up @@ -2082,7 +2082,8 @@ static int spl_filesystem_file_read(spl_filesystem_object *intern, int silent TS
intern->u.file.current_line = estrdup("");
intern->u.file.current_line_len = 0;
} else {
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE)) {
if (SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_DROP_NEW_LINE) && !SPL_HAS_FLAG(intern->flags, SPL_FILE_OBJECT_READ_CSV)) {
/* when SPL_FILE_OBJECT_READ_CSV is set, a newline does not necessarily mean the end of the row, so don't drop. CSV rows can contain multiple quoted newlines. */
line_len = strcspn(buf, "\r\n");
buf[line_len] = '\0';
}
Expand Down
69 changes: 69 additions & 0 deletions ext/spl/tests/SplFileObject_fgetcsv_drop_new_line.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
--TEST--
SplFileObject::fgetcsv default path
--FILE--
<?php
$csv = <<<CSV
"field1","field2","field3"
"field
4","field
5","field6
"

CSV;
file_put_contents('SplFileObject__fgetcsv_dropnewline.csv', $csv);

// Read the CSV without DROP_NEW_LINES first
$fo = new SplFileObject('SplFileObject__fgetcsv_dropnewline.csv');
$fo->setFlags(SplFileObject::READ_CSV);
var_dump($fo->fgetcsv());
var_dump($fo->fgetcsv());

// Read the CSV without DROP_NEW_LINES first
$fo = new SplFileObject('SplFileObject__fgetcsv_dropnewline.csv');
$fo->setFlags(SplFileObject::READ_CSV | SplFileObject::DROP_NEW_LINE);
var_dump($fo->fgetcsv());
var_dump($fo->fgetcsv());
?>
--CLEAN--
<?php
unlink('SplFileObject__fgetcsv_dropnewline.csv');
?>
--EXPECTF--
array(3) {
[0]=>
string(6) "field1"
[1]=>
string(6) "field2"
[2]=>
string(6) "field3"
}
array(3) {
[0]=>
string(7) "field
4"
[1]=>
string(7) "field
5"
[2]=>
string(7) "field6
"
}
array(3) {
[0]=>
string(6) "field1"
[1]=>
string(6) "field2"
[2]=>
string(6) "field3"
}
array(3) {
[0]=>
string(7) "field
4"
[1]=>
string(7) "field
5"
[2]=>
string(7) "field6
"
}