From 4dfc8ed0747f5d7cd10ba35621add0fe4891a545 Mon Sep 17 00:00:00 2001 From: Yonik Seeley Date: Thu, 15 Jul 2010 01:32:55 +0000 Subject: [PATCH] SANDBOX-313: Endless loops in CSV parser when last line is comment git-svn-id: https://svn.apache.org/repos/asf/commons/sandbox/csv/trunk@964273 13f79535-47bb-0310-9956-ffa450edef68 --- src/java/org/apache/commons/csv/CSVParser.java | 2 +- .../org/apache/commons/csv/CSVParserTest.java | 16 +++++++++++++++- .../org/apache/commons/csv/CSVPrinterTest.java | 6 ++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/java/org/apache/commons/csv/CSVParser.java b/src/java/org/apache/commons/csv/CSVParser.java index 1813afeb..a7fc1936 100644 --- a/src/java/org/apache/commons/csv/CSVParser.java +++ b/src/java/org/apache/commons/csv/CSVParser.java @@ -343,7 +343,7 @@ public class CSVParser { } // important: make sure a new char gets consumed in each iteration - while (!tkn.isReady) { + while (!tkn.isReady && tkn.type != TT_EOF) { // ignore whitespaces at beginning of a token while (strategy.getIgnoreLeadingWhitespaces() && isWhitespace(c) && !eol) { wsBuf.append((char) c); diff --git a/src/test/org/apache/commons/csv/CSVParserTest.java b/src/test/org/apache/commons/csv/CSVParserTest.java index 08c0454d..a838be1b 100644 --- a/src/test/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/org/apache/commons/csv/CSVParserTest.java @@ -497,7 +497,7 @@ public class CSVParserTest extends TestCase { String[][] res = { { "a", "b" }, { "\n", " " }, - { "", "#" }, // WARNING: TODO: this causes a hang if comments are enabled + { "", "#" }, }; CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY; @@ -510,6 +510,20 @@ public class CSVParserTest extends TestCase { if (!CSVPrinterTest.equals(res, tmp)) { assertTrue(false); } + + String[][] res_comments = { + { "a", "b" }, + { "\n", " " }, + { ""}, + }; + + strategy = new CSVStrategy(',','"','#'); + parser = new CSVParser(new StringReader(code), strategy); + tmp = parser.getAllValues(); + + if (!CSVPrinterTest.equals(res_comments, tmp)) { + assertTrue(false); + } } diff --git a/src/test/org/apache/commons/csv/CSVPrinterTest.java b/src/test/org/apache/commons/csv/CSVPrinterTest.java index 6e9815d7..b4fd4e75 100644 --- a/src/test/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/org/apache/commons/csv/CSVPrinterTest.java @@ -133,9 +133,15 @@ public class CSVPrinterTest extends TestCase { } public static boolean equals(String[][] a, String[][] b) { + if (a.length != b.length) { + return false; + } for (int i=0; i