From 99af18a04755562344e6ad60f61e29149fd98dfd Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 5 Mar 2015 10:57:33 +0000 Subject: [PATCH 1/3] Add test case for bug #69181 Running this test fails because when DROP_NEW_LINES flag is present when reading CSV files, fields containing quoted new lines are not anticipated. --- .../SplFileObject_fgetcsv_drop_new_line.phpt | 69 +++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 ext/spl/tests/SplFileObject_fgetcsv_drop_new_line.phpt diff --git a/ext/spl/tests/SplFileObject_fgetcsv_drop_new_line.phpt b/ext/spl/tests/SplFileObject_fgetcsv_drop_new_line.phpt new file mode 100644 index 0000000000000..03655ee51a993 --- /dev/null +++ b/ext/spl/tests/SplFileObject_fgetcsv_drop_new_line.phpt @@ -0,0 +1,69 @@ +--TEST-- +SplFileObject::fgetcsv default path +--FILE-- +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-- + +--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 +" +} \ No newline at end of file From c777becaf68e00dad1bfc74615c02312970dd2d7 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 5 Mar 2015 10:59:40 +0000 Subject: [PATCH 2/3] Skip DROP_NEW_LINE flag when READ_CSV flag is in use Fixes bug #69181 (csv parsing drops newlines within fields with SplFileObject::DROP_NEW_LINE) 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. --- ext/spl/spl_directory.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ext/spl/spl_directory.c b/ext/spl/spl_directory.c index 34f0415b53348..b0cd3ba744733 100644 --- a/ext/spl/spl_directory.c +++ b/ext/spl/spl_directory.c @@ -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'; } From cc375cf1e0ab198847317bfa631b40c6eeb71c60 Mon Sep 17 00:00:00 2001 From: Greg Bowler Date: Thu, 5 Mar 2015 11:43:56 +0000 Subject: [PATCH 3/3] Update NEWS with bug #69181 fix --- NEWS | 1 + 1 file changed, 1 insertion(+) diff --git a/NEWS b/NEWS index 5eec926c32cb7..b34ab42447034 100644 --- a/NEWS +++ b/NEWS @@ -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)