Skip to content

Commit 10c8db1

Browse files
committed
Add test of illegal replacement char
1 parent 84a0d90 commit 10c8db1

2 files changed

Lines changed: 14 additions & 2 deletions

File tree

src/main/java/org/apache/commons/io/FileSystem.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,8 @@ public String toLegalFileName(final String candidate, final char replacement) {
211211
if (isIllegalFileNameChar(replacement)) {
212212
throw new IllegalArgumentException(
213213
String.format("The replacement character '%s' cannot be one of the %s illegal characters: %s",
214-
replacement, name(), Arrays.toString(illegalFileNameChars)));
214+
// %s does not work properly with NUL
215+
replacement == '\0' ? "\\0" : replacement, name(), Arrays.toString(illegalFileNameChars)));
215216
}
216217
final String truncated = candidate.length() > maxFileNameLength ? candidate.substring(0, maxFileNameLength)
217218
: candidate;

src/test/java/org/apache/commons/io/FileSystemTestCase.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ public void testToLegalFileNameWindows() {
5252
for (char i = '0'; i < '9'; i++) {
5353
Assert.assertEquals(i, fs.toLegalFileName(String.valueOf(i), replacement).charAt(0));
5454
}
55-
}
55+
}
5656

5757
@Test
5858
public void testIsLegalName() {
@@ -63,4 +63,15 @@ public void testIsLegalName() {
6363
Assert.assertTrue(fs.name(), fs.isLegalFileName("0")); // Assume simple name always legal
6464
}
6565
}
66+
67+
@Test
68+
public void testReplacementWithNUL() {
69+
for (FileSystem fs : FileSystem.values()) {
70+
try {
71+
fs.toLegalFileName("Test", '\0'); // Assume NUL is always illegal
72+
} catch (IllegalArgumentException iae) {
73+
Assert.assertTrue(iae.getMessage(), iae.getMessage().startsWith("The replacement character '\\0'"));
74+
}
75+
}
76+
}
6677
}

0 commit comments

Comments
 (0)