File tree Expand file tree Collapse file tree
main/java/org/apache/commons/csv
test/java/org/apache/commons/csv Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -153,6 +153,7 @@ boolean isDelimiter(final int ch) throws IOException {
153153 isLastTokenDelimiter = true ;
154154 return true ;
155155 }
156+ Arrays .fill (delimiterBuf , '\0' );
156157 reader .peek (delimiterBuf );
157158 for (int i = 0 ; i < delimiterBuf .length ; i ++) {
158159 if (delimiterBuf [i ] != delimiter [i + 1 ]) {
@@ -274,7 +275,6 @@ Token nextToken(final Token token) throws IOException {
274275 token .type = Token .Type .COMMENT ;
275276 return token ;
276277 }
277- Arrays .fill (delimiterBuf , '\0' );
278278 // Important: make sure a new char gets consumed in each iteration
279279 while (token .type == Token .Type .INVALID ) {
280280 // ignore whitespaces at beginning of a token
Original file line number Diff line number Diff line change @@ -433,6 +433,19 @@ void testPartialMultiCharacterDelimiterAtEOF() throws IOException {
433433 }
434434 }
435435
436+ /**
437+ * A truncated multi-character delimiter at EOF must not be accepted by reusing the look-ahead buffer left dirty by an
438+ * earlier non-matching peek in the same token (CSV-324 only cleared the buffer once per token).
439+ */
440+ @ Test
441+ void testPartialMultiCharacterDelimiterAtEOFAfterMismatch () throws IOException {
442+ final CSVFormat format = CSVFormat .DEFAULT .builder ().setDelimiter ("[|]" ).get ();
443+ // The "[a]" peek leaves ']' in the look-ahead buffer; the trailing "[|" must not match "[|]".
444+ try (Lexer lexer = createLexer ("x[a][|" , format )) {
445+ assertNextToken (EOF , "x[a][|" , lexer );
446+ }
447+ }
448+
436449 @ Test
437450 void testReadEscapeBackspace () throws IOException {
438451 try (Lexer lexer = createLexer ("b" , CSVFormat .DEFAULT .withEscape ('\b' ))) {
You can’t perform that action at this time.
0 commit comments