Skip to content

Commit 4dfc8ed

Browse files
committed
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
1 parent 02b2146 commit 4dfc8ed

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/java/org/apache/commons/csv/CSVParser.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ protected Token nextToken(Token tkn) throws IOException {
343343
}
344344

345345
// important: make sure a new char gets consumed in each iteration
346-
while (!tkn.isReady) {
346+
while (!tkn.isReady && tkn.type != TT_EOF) {
347347
// ignore whitespaces at beginning of a token
348348
while (strategy.getIgnoreLeadingWhitespaces() && isWhitespace(c) && !eol) {
349349
wsBuf.append((char) c);

src/test/org/apache/commons/csv/CSVParserTest.java

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ public void testDefaultStrategy() throws IOException {
497497
String[][] res = {
498498
{ "a", "b" },
499499
{ "\n", " " },
500-
{ "", "#" }, // WARNING: TODO: this causes a hang if comments are enabled
500+
{ "", "#" },
501501
};
502502

503503
CSVStrategy strategy = CSVStrategy.DEFAULT_STRATEGY;
@@ -510,6 +510,20 @@ public void testDefaultStrategy() throws IOException {
510510
if (!CSVPrinterTest.equals(res, tmp)) {
511511
assertTrue(false);
512512
}
513+
514+
String[][] res_comments = {
515+
{ "a", "b" },
516+
{ "\n", " " },
517+
{ ""},
518+
};
519+
520+
strategy = new CSVStrategy(',','"','#');
521+
parser = new CSVParser(new StringReader(code), strategy);
522+
tmp = parser.getAllValues();
523+
524+
if (!CSVPrinterTest.equals(res_comments, tmp)) {
525+
assertTrue(false);
526+
}
513527
}
514528

515529

src/test/org/apache/commons/csv/CSVPrinterTest.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,15 @@ public void doOneRandom() throws Exception {
133133
}
134134

135135
public static boolean equals(String[][] a, String[][] b) {
136+
if (a.length != b.length) {
137+
return false;
138+
}
136139
for (int i=0; i<a.length; i++) {
137140
String[] linea = a[i];
138141
String[] lineb = b[i];
142+
if (linea.length != lineb.length) {
143+
return false;
144+
}
139145
for (int j=0; j<linea.length; j++) {
140146
String aval = linea[j];
141147
String bval = lineb[j];

0 commit comments

Comments
 (0)