Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/main/java/org/apache/commons/csv/Lexer.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,7 @@ boolean isEscape(final int ch) {
* @throws IOException If an I/O error occurs.
*/
boolean isEscapeDelimiter() throws IOException {
Arrays.fill(escapeDelimiterBuf, '\0');
reader.peek(escapeDelimiterBuf);
if (escapeDelimiterBuf[0] != delimiter[0]) {
return false;
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/org/apache/commons/csv/CSVParserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1665,6 +1665,20 @@ void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
}
}

/**
* A truncated escaped multi-character delimiter at EOF must stay literal data and not be completed from a stale
* escape delimiter look-ahead.
*/
@Test
void testPartialEscapedMultiCharacterDelimiterAtEOF() throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
try (CSVParser parser = format.parse(new StringReader("x![!|!]y![!|"))) {
final CSVRecord record = parser.nextRecord();
assertEquals("x[|]y![!|", record.get(0));
assertEquals(1, record.size());
}
}

@Test
void testProvidedHeader() throws Exception {
final Reader in = new StringReader("a,b,c\n1,2,3\nx,y,z");
Expand Down
12 changes: 12 additions & 0 deletions src/test/java/org/apache/commons/csv/LexerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,18 @@ void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
}
}

/**
* A truncated escaped multi-character delimiter at EOF must not be accepted by reusing the previous escape delimiter
* look-ahead in {@link Lexer#isEscapeDelimiter()}.
*/
@Test
void testPartialEscapedMultiCharacterDelimiterAtEOF() throws IOException {
final CSVFormat format = CSVFormat.DEFAULT.builder().setDelimiter("[|]").setEscape('!').get();
try (Lexer lexer = createLexer("x![!|!]y![!|", format)) {
assertNextToken(EOF, "x[|]y![!|", lexer);
}
}

@Test
void testReadEscapeBackspace() throws IOException {
try (Lexer lexer = createLexer("b", CSVFormat.DEFAULT.withEscape('\b'))) {
Expand Down