From 9c702bd405d3d246f2d6d7e0f2f07f9753cc8373 Mon Sep 17 00:00:00 2001 From: Peter Hull Date: Thu, 8 Sep 2022 21:54:29 +0100 Subject: [PATCH] Guard against NPE in createHeaders Also, formatting and whitespace changes as requested in code review. --- src/main/java/org/apache/commons/csv/CSVParser.java | 11 ++++++----- .../java/org/apache/commons/csv/CSVParserTest.java | 13 +++++++++++++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/main/java/org/apache/commons/csv/CSVParser.java b/src/main/java/org/apache/commons/csv/CSVParser.java index e06b95f1..94e1dd04 100644 --- a/src/main/java/org/apache/commons/csv/CSVParser.java +++ b/src/main/java/org/apache/commons/csv/CSVParser.java @@ -352,6 +352,7 @@ public final class CSVParser implements Iterable, Closeable { return new CSVParser(new InputStreamReader(url.openStream(), charset), format); } + private String headerComment; private String trailerComment; @@ -487,8 +488,10 @@ public final class CSVParser implements Iterable, Closeable { } } else { if (this.format.getSkipHeaderRecord()) { - final CSVRecord csvRecord = this.nextRecord(); - headerComment = csvRecord.getComment(); + final CSVRecord nextRecord = this.nextRecord(); + if (nextRecord != null) { + headerComment = nextRecord.getComment(); + } } headerRecord = formatHeader; } @@ -764,10 +767,8 @@ public final class CSVParser implements Iterable, Closeable { case EOF: if (this.reusableToken.isReady) { this.addRecordValue(true); - } else { - if (sb != null) { + } else if (sb != null) { trailerComment = sb.toString(); - } } break; case INVALID: diff --git a/src/test/java/org/apache/commons/csv/CSVParserTest.java b/src/test/java/org/apache/commons/csv/CSVParserTest.java index 7d2dfbdb..46522b11 100644 --- a/src/test/java/org/apache/commons/csv/CSVParserTest.java +++ b/src/test/java/org/apache/commons/csv/CSVParserTest.java @@ -1396,6 +1396,7 @@ public class CSVParserTest { .setCommentMarker('#') .setHeader("A", "B") .build(); + @Test public void testGetHeaderComment_NoComment1() throws IOException { @@ -1406,6 +1407,7 @@ public class CSVParserTest { assertNull(parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_HeaderComment1() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) { @@ -1415,6 +1417,7 @@ public class CSVParserTest { assertEquals("header comment", parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_HeaderTrailerComment() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) { @@ -1424,6 +1427,7 @@ public class CSVParserTest { assertEquals("multi-line"+LF+"header comment", parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_NoComment2() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER)) { @@ -1433,6 +1437,7 @@ public class CSVParserTest { assertNull(parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_HeaderComment2() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) { @@ -1442,6 +1447,7 @@ public class CSVParserTest { assertEquals("header comment", parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_NoComment3() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) { @@ -1451,6 +1457,7 @@ public class CSVParserTest { assertNull(parser.getHeaderComment()); } } + @Test public void testGetHeaderComment_HeaderComment3() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) { @@ -1469,6 +1476,7 @@ public class CSVParserTest { assertNull(parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_HeaderTrailerComment1() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) { @@ -1477,6 +1485,7 @@ public class CSVParserTest { assertEquals("comment", parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_MultilineComment() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) { @@ -1485,6 +1494,7 @@ public class CSVParserTest { assertEquals("multi-line"+LF+"comment", parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_HeaderComment2() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) { @@ -1493,6 +1503,7 @@ public class CSVParserTest { assertNull(parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_HeaderTrailerComment2() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER)) { @@ -1501,6 +1512,7 @@ public class CSVParserTest { assertEquals("comment", parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_HeaderComment3() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) { @@ -1509,6 +1521,7 @@ public class CSVParserTest { assertNull(parser.getTrailerComment()); } } + @Test public void testGetTrailerComment_HeaderTrailerComment3() throws IOException { try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {