Skip to content

Commit 9c702bd

Browse files
committed
Guard against NPE in createHeaders
Also, formatting and whitespace changes as requested in code review.
1 parent 82ca035 commit 9c702bd

2 files changed

Lines changed: 19 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -352,6 +352,7 @@ public static CSVParser parse(final URL url, final Charset charset, final CSVFor
352352

353353
return new CSVParser(new InputStreamReader(url.openStream(), charset), format);
354354
}
355+
355356
private String headerComment;
356357

357358
private String trailerComment;
@@ -487,8 +488,10 @@ private Headers createHeaders() throws IOException {
487488
}
488489
} else {
489490
if (this.format.getSkipHeaderRecord()) {
490-
final CSVRecord csvRecord = this.nextRecord();
491-
headerComment = csvRecord.getComment();
491+
final CSVRecord nextRecord = this.nextRecord();
492+
if (nextRecord != null) {
493+
headerComment = nextRecord.getComment();
494+
}
492495
}
493496
headerRecord = formatHeader;
494497
}
@@ -764,10 +767,8 @@ CSVRecord nextRecord() throws IOException {
764767
case EOF:
765768
if (this.reusableToken.isReady) {
766769
this.addRecordValue(true);
767-
} else {
768-
if (sb != null) {
770+
} else if (sb != null) {
769771
trailerComment = sb.toString();
770-
}
771772
}
772773
break;
773774
case INVALID:

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,6 +1396,7 @@ private void validateRecordPosition(final String lineSeparator) throws IOExcepti
13961396
.setCommentMarker('#')
13971397
.setHeader("A", "B")
13981398
.build();
1399+
13991400
@Test
14001401
public void testGetHeaderComment_NoComment1() throws IOException {
14011402

@@ -1406,6 +1407,7 @@ public void testGetHeaderComment_NoComment1() throws IOException {
14061407
assertNull(parser.getHeaderComment());
14071408
}
14081409
}
1410+
14091411
@Test
14101412
public void testGetHeaderComment_HeaderComment1() throws IOException {
14111413
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_AUTO_HEADER)) {
@@ -1415,6 +1417,7 @@ public void testGetHeaderComment_HeaderComment1() throws IOException {
14151417
assertEquals("header comment", parser.getHeaderComment());
14161418
}
14171419
}
1420+
14181421
@Test
14191422
public void testGetHeaderComment_HeaderTrailerComment() throws IOException {
14201423
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
@@ -1424,6 +1427,7 @@ public void testGetHeaderComment_HeaderTrailerComment() throws IOException {
14241427
assertEquals("multi-line"+LF+"header comment", parser.getHeaderComment());
14251428
}
14261429
}
1430+
14271431
@Test
14281432
public void testGetHeaderComment_NoComment2() throws IOException {
14291433
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER)) {
@@ -1433,6 +1437,7 @@ public void testGetHeaderComment_NoComment2() throws IOException {
14331437
assertNull(parser.getHeaderComment());
14341438
}
14351439
}
1440+
14361441
@Test
14371442
public void testGetHeaderComment_HeaderComment2() throws IOException {
14381443
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
@@ -1442,6 +1447,7 @@ public void testGetHeaderComment_HeaderComment2() throws IOException {
14421447
assertEquals("header comment", parser.getHeaderComment());
14431448
}
14441449
}
1450+
14451451
@Test
14461452
public void testGetHeaderComment_NoComment3() throws IOException {
14471453
try (CSVParser parser = CSVParser.parse(CSV_INPUT_NO_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
@@ -1451,6 +1457,7 @@ public void testGetHeaderComment_NoComment3() throws IOException {
14511457
assertNull(parser.getHeaderComment());
14521458
}
14531459
}
1460+
14541461
@Test
14551462
public void testGetHeaderComment_HeaderComment3() throws IOException {
14561463
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
@@ -1469,6 +1476,7 @@ public void testGetTrailerComment_HeaderComment1() throws IOException {
14691476
assertNull(parser.getTrailerComment());
14701477
}
14711478
}
1479+
14721480
@Test
14731481
public void testGetTrailerComment_HeaderTrailerComment1() throws IOException {
14741482
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
@@ -1477,6 +1485,7 @@ public void testGetTrailerComment_HeaderTrailerComment1() throws IOException {
14771485
assertEquals("comment", parser.getTrailerComment());
14781486
}
14791487
}
1488+
14801489
@Test
14811490
public void testGetTrailerComment_MultilineComment() throws IOException {
14821491
try (CSVParser parser = CSVParser.parse(CSV_INPUT_MULTILINE_HEADER_TRAILER_COMMENT, FORMAT_AUTO_HEADER)) {
@@ -1485,6 +1494,7 @@ public void testGetTrailerComment_MultilineComment() throws IOException {
14851494
assertEquals("multi-line"+LF+"comment", parser.getTrailerComment());
14861495
}
14871496
}
1497+
14881498
@Test
14891499
public void testGetTrailerComment_HeaderComment2() throws IOException {
14901500
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
@@ -1493,6 +1503,7 @@ public void testGetTrailerComment_HeaderComment2() throws IOException {
14931503
assertNull(parser.getTrailerComment());
14941504
}
14951505
}
1506+
14961507
@Test
14971508
public void testGetTrailerComment_HeaderTrailerComment2() throws IOException {
14981509
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER)) {
@@ -1501,6 +1512,7 @@ public void testGetTrailerComment_HeaderTrailerComment2() throws IOException {
15011512
assertEquals("comment", parser.getTrailerComment());
15021513
}
15031514
}
1515+
15041516
@Test
15051517
public void testGetTrailerComment_HeaderComment3() throws IOException {
15061518
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {
@@ -1509,6 +1521,7 @@ public void testGetTrailerComment_HeaderComment3() throws IOException {
15091521
assertNull(parser.getTrailerComment());
15101522
}
15111523
}
1524+
15121525
@Test
15131526
public void testGetTrailerComment_HeaderTrailerComment3() throws IOException {
15141527
try (CSVParser parser = CSVParser.parse(CSV_INPUT_HEADER_TRAILER_COMMENT, FORMAT_EXPLICIT_HEADER_NOSKIP)) {

0 commit comments

Comments
 (0)