Skip to content

Commit 1e3de12

Browse files
committed
clear escape delimiter buffer before peek in isEscapeDelimiter
1 parent 361056d commit 1e3de12

3 files changed

Lines changed: 27 additions & 0 deletions

File tree

src/main/java/org/apache/commons/csv/Lexer.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,7 @@ boolean isEscape(final int ch) {
191191
* @throws IOException If an I/O error occurs.
192192
*/
193193
boolean isEscapeDelimiter() throws IOException {
194+
Arrays.fill(escapeDelimiterBuf, '\0');
194195
reader.peek(escapeDelimiterBuf);
195196
if (escapeDelimiterBuf[0] != delimiter[0]) {
196197
return false;

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1665,6 +1665,20 @@ void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
16651665
}
16661666
}
16671667

1668+
/**
1669+
* A truncated escaped multi-character delimiter at EOF must stay literal data and not be completed from a stale
1670+
* escape delimiter look-ahead.
1671+
*/
1672+
@Test
1673+
void testPartialEscapedMultiCharacterDelimiterAtEOF() throws IOException {
1674+
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
1675+
try (CSVParser parser = format.parse(new StringReader("x![!|!]y![!|"))) {
1676+
final CSVRecord record = parser.nextRecord();
1677+
assertEquals("x[|]y![!|", record.get(0));
1678+
assertEquals(1, record.size());
1679+
}
1680+
}
1681+
16681682
@Test
16691683
void testProvidedHeader() throws Exception {
16701684
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,18 @@ void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
421421
}
422422
}
423423

424+
/**
425+
* A truncated escaped multi-character delimiter at EOF must not be accepted by reusing the previous escape delimiter
426+
* look-ahead in {@link Lexer#isEscapeDelimiter()}.
427+
*/
428+
@Test
429+
void testPartialEscapedMultiCharacterDelimiterAtEOF() throws IOException {
430+
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
431+
try (Lexer lexer = createLexer("x![!|!]y![!|", format)) {
432+
assertNextToken(EOF, "x[|]y![!|", lexer);
433+
}
434+
}
435+
424436
@Test
425437
void testReadEscapeBackspace() throws IOException {
426438
try (Lexer lexer = createLexer("b", CSVFormat.DEFAULT.withEscape('\b'))) {

0 commit comments

Comments
 (0)