Warning about .clone()
See
https://lists.apache.org/thread.html/e7c54a595325f1faf7c78b86e76c048c98a914fbb869b8f6d648e4f5@%3Cdev.commons.apache.org%3E
diff --git a/src/main/java/org/apache/commons/csv/CSVFormat.java b/src/main/java/org/apache/commons/csv/CSVFormat.java
index 6a0713d..5c11ebb 100644
--- a/src/main/java/org/apache/commons/csv/CSVFormat.java
+++ b/src/main/java/org/apache/commons/csv/CSVFormat.java
@@ -648,6 +648,7 @@
this.recordSeparator = recordSeparator;
this.nullString = nullString;
this.headerComments = toStringArray(headerComments);
+ // Note: .clone() is generally not-OK, but this is safe as Strings are immutable
this.header = header == null ? null : header.clone();
this.skipHeaderRecord = skipHeaderRecord;
this.ignoreHeaderCase = ignoreHeaderCase;
@@ -798,6 +799,7 @@
* @return a copy of the header array; {@code null} if disabled, the empty array if to be read from the file
*/
public String[] getHeader() {
+ // Note: .clone() is generally not-OK, but this is safe as Strings are immutable
return header != null ? header.clone() : null;
}
diff --git a/src/main/java/org/apache/commons/csv/CSVRecord.java b/src/main/java/org/apache/commons/csv/CSVRecord.java
index 2be5c49..6f5e768 100644
--- a/src/main/java/org/apache/commons/csv/CSVRecord.java
+++ b/src/main/java/org/apache/commons/csv/CSVRecord.java
@@ -199,6 +199,10 @@
public final CSVRecord immutable() {
if (isMutable()) {
// Subclass is probably CSVMutableRecord, freeze values
+ //
+ // Note: Normally we should not use .clone() as it has many
+ // issues and pitfalls - here we have an array of immutable Strings
+ // so it's OK.
String[] frozenValue = values.clone();
return new CSVRecord(frozenValue, mapping, comment, recordNumber, characterPosition);
} else {
@@ -260,7 +264,10 @@
if (isMutable()) {
return this;
}
- String[] newValues = values.clone();
+ // Note: Normally we should not use .clone() as it has many
+ // issues and pitfalls - here we have an array of immutable Strings
+ // so it's OK.
+ String[] newValues = values.clone();
return new CSVMutableRecord(newValues, mapping, comment, recordNumber, characterPosition);
}