From 488b8f9d9ba1230c9eb076acac09283c78c7518b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christian=20Bl=C3=A4ul?= Date: Sun, 21 Jan 2018 20:55:18 +0100 Subject: [PATCH] Prevent an "Uninitialized string offset" error Got triggered by testMissingEndingLineBreak. --- parsecsv.lib.php | 1 + tests/methods/ParseTest.php | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/parsecsv.lib.php b/parsecsv.lib.php index 4c5b7cc..03b17fc 100644 --- a/parsecsv.lib.php +++ b/parsecsv.lib.php @@ -598,6 +598,7 @@ class parseCSV { // data does not end with a line feed or carriage return character. $lch = $data{$strlen - 1}; if ($lch != "\n" && $lch != "\r") { + $data .= "\n"; $strlen++; } diff --git a/tests/methods/ParseTest.php b/tests/methods/ParseTest.php index f05781a..c7f8fcb 100644 --- a/tests/methods/ParseTest.php +++ b/tests/methods/ParseTest.php @@ -62,6 +62,21 @@ class ParseTest extends PHPUnit\Framework\TestCase { $this->assertEquals($expected_data, $actual_data); } + public function testMissingEndingLineBreak() { + $this->csv->heading = false; + $this->csv->enclosure = '"'; + $sInput = "86545235689,a\r\n34365587654,b\r\n13469874576,\"c\r\nd\""; + $expected_data = [86545235689, 34365587654, 13469874576]; + $actual_data = $this->csv->parse_string($sInput); + $actual_column = array_map('reset', $actual_data); + $this->assertEquals($expected_data, $actual_column); + $this->assertEquals([ + 'a', + 'b', + "c\r\nd", + ], array_map('next', $actual_data)); + } + public function test_sep_row_auto_detection_UTF8() { $this->_autoparse_magazine_file( __DIR__ . '/../example_files/UTF-8_with_BOM_and_sep_row.csv');