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
This commit is contained in:
Yonik Seeley 2010-07-15 01:32:55 +00:00
parent 02b21463e6
commit 4dfc8ed074
3 changed files with 22 additions and 2 deletions

View File

@ -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);

View File

@ -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);
}
}

View File

@ -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<a.length; i++) {
String[] linea = a[i];
String[] lineb = b[i];
if (linea.length != lineb.length) {
return false;
}
for (int j=0; j<linea.length; j++) {
String aval = linea[j];
String bval = lineb[j];