From 6e5580803ba9f46552677a5a858804888521ed98 Mon Sep 17 00:00:00 2001 From: simpleqt <2015900818@qq.com> Date: Mon, 16 Nov 2026 00:26:55 +0800 Subject: [PATCH] Escape quote characters in the quoted null marker CSVFormat.Builder builds quotedNullString, the token QuoteMode.ALL uses for null values, by concatenating the quote character around the null marker. A marker that contains the quote character closes its own encapsulation, so CSVPrinter emits CSV that its parser rejects with "Invalid character between encapsulated token and delimiter". CSV-328 already fixed an invalid quoted null marker built in the wrong setter order; this escapes embedded quote characters too, the same way printWithQuotes does for a value. Assisted-By: DeepSeek Harness --- .../java/org/apache/commons/csv/CSVFormat.java | 7 +++++-- .../org/apache/commons/csv/CSVPrinterTest.java | 17 +++++++++++++++++ 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java index cc3192a02..914a6418e 100644 --- a/src/main/java/org/apache/commons/csv/CSVFormat.java +++ b/src/main/java/org/apache/commons/csv/CSVFormat.java @@ -815,8 +815,11 @@ public Builder setQuote(final Character quoteCharacter) { } private Builder setQuotedNullString() { - final Character quote = quoteCharacter != null ? quoteCharacter : Constants.DOUBLE_QUOTE_CHAR; - this.quotedNullString = quote + nullString + quote; + final char quote = quoteCharacter != null ? quoteCharacter.charValue() : Constants.DOUBLE_QUOTE_CHAR.charValue(); + // Double the quote character inside the null marker, otherwise the quoted marker closes its own + // encapsulation and the emitted token is invalid or spans several fields. + final String marker = nullString != null ? nullString : "null"; + this.quotedNullString = quote + marker.replace(String.valueOf(quote), String.valueOf(quote) + quote) + quote; return this; } diff --git a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java index 66064f448..40456a5de 100644 --- a/src/test/java/org/apache/commons/csv/CSVPrinterTest.java +++ b/src/test/java/org/apache/commons/csv/CSVPrinterTest.java @@ -2291,6 +2291,23 @@ void testTrimOnTwoColumns() throws IOException { assertEquals("A,B", sw.toString()); } } + @Test + void testQuotedNullStringEscapesEmbeddedQuoteCharacter() throws IOException { + // QuoteMode.ALL writes null as the quoted null marker. The marker must escape its own quote characters, + // otherwise it closes its encapsulation early and the printer emits CSV the parser rejects. + final CSVFormat format = CSVFormat.DEFAULT.builder().setNullString("N\"A").setQuoteMode(QuoteMode.ALL).get(); + final StringWriter sw = new StringWriter(); + try (CSVPrinter printer = new CSVPrinter(sw, format)) { + printer.printRecord("x", null); + } + assertEquals("\"x\",\"N\"\"A\"" + RECORD_SEPARATOR, sw.toString()); + try (CSVParser parser = CSVParser.parse(sw.toString(), format)) { + final CSVRecord record = parser.getRecords().get(0); + assertEquals(2, record.size()); + assertEquals("x", record.get(0)); + assertNull(record.get(1)); + } + } private String[] toFirstRecordValues(final String expected, final CSVFormat format) throws IOException { try (CSVParser parser = CSVParser.parse(expected, format)) {