Add test of illegal replacement char
MODIFY
src/main/java/org/apache/commons/io/FileSystem.java
src/main/java/org/apache/commons/io/FileSystem.java
@@ -211,7 +211,8 @@ public String toLegalFileName(final String candidate, final char replacement) {
if (isIllegalFileNameChar(replacement)) {
throw new IllegalArgumentException(
String.format("The replacement character '%s' cannot be one of the %s illegal characters: %s",
- replacement, name(), Arrays.toString(illegalFileNameChars)));
+ // %s does not work properly with NUL
+ replacement == '\0' ? "\\0" : replacement, name(), Arrays.toString(illegalFileNameChars)));
}
final String truncated = candidate.length() > maxFileNameLength ? candidate.substring(0, maxFileNameLength)
: candidate;

MODIFY
src/test/java/org/apache/commons/io/FileSystemTestCase.java
src/test/java/org/apache/commons/io/FileSystemTestCase.java
@@ -63,4 +63,15 @@ public void testIsLegalName() {
Assert.assertTrue(fs.name(), fs.isLegalFileName("0")); // Assume simple name always legal
}
}
+
+ @Test
+ public void testReplacementWithNUL() {
+ for (FileSystem fs : FileSystem.values()) {
+ try {
+ fs.toLegalFileName("Test", '\0'); // Assume NUL is always illegal
+ } catch (IllegalArgumentException iae) {
+ Assert.assertTrue(iae.getMessage(), iae.getMessage().startsWith("The replacement character '\\0'"));
+ }
+ }
+ }
}

Add test of illegal replacement char