Skip to content

Commit 5786200

Browse files
author
Brian Burkhalter
committed
8354450: A File should be invalid if an element of its name sequence ends with a space
Reviewed-by: alanb
1 parent 04c1546 commit 5786200

File tree

2 files changed

+96
-12
lines changed

2 files changed

+96
-12
lines changed

src/java.base/windows/classes/java/io/WinNTFileSystem.java

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -365,27 +365,28 @@ public boolean isAbsolute(File f) {
365365

366366
@Override
367367
public boolean isInvalid(File f) {
368-
if (f.getPath().indexOf('\u0000') >= 0)
368+
final String pathname = f.getPath();
369+
370+
// Invalid if the pathname string contains a null character or if
371+
// any name in the pathname's name sequence ends with a space
372+
if (pathname.indexOf('\u0000') >= 0 || pathname.endsWith(" ")
373+
|| pathname.contains(" \\"))
369374
return true;
370375

376+
// The remaining checks are irrelevant for alternate data streams (ADS)
371377
if (ENABLE_ADS)
372378
return false;
373379

374-
// Invalid if there is a ":" at a position greater than 1, or if there
380+
// Invalid if there is a ":" at a position other than 1, or if there
375381
// is a ":" at position 1 and the first character is not a letter
376-
String pathname = f.getPath();
377382
int lastColon = pathname.lastIndexOf(":");
383+
if (lastColon >= 0 &&
384+
(lastColon != 1 || !isLetter(pathname.charAt(0))))
385+
return true;
378386

379-
// Valid if there is no ":" present or if the last ":" present is
380-
// at index 1 and the first character is a latter
381-
if (lastColon < 0 ||
382-
(lastColon == 1 && isLetter(pathname.charAt(0))))
383-
return false;
384-
385-
// Invalid if path creation fails
386-
Path path = null;
387+
// Invalid if the path string cannot be converted to a Path
387388
try {
388-
path = sun.nio.fs.DefaultFileSystemProvider.theFileSystem().getPath(pathname);
389+
Path path = sun.nio.fs.DefaultFileSystemProvider.theFileSystem().getPath(pathname);
389390
return false;
390391
} catch (InvalidPathException ignored) {
391392
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation.
8+
*
9+
* This code is distributed in the hope that it will be useful, but WITHOUT
10+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12+
* version 2 for more details (a copy is included in the LICENSE file that
13+
* accompanied this code).
14+
*
15+
* You should have received a copy of the GNU General Public License version
16+
* 2 along with this work; if not, write to the Free Software Foundation,
17+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18+
*
19+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20+
* or visit www.oracle.com if you need additional information or have any
21+
* questions.
22+
*/
23+
24+
import java.io.File;
25+
import java.io.IOException;
26+
27+
import org.junit.jupiter.api.Test;
28+
import static org.junit.jupiter.api.Assertions.assertFalse;
29+
import static org.junit.jupiter.api.Assertions.assertTrue;
30+
31+
/* @test
32+
* @bug 8354450
33+
* @requires os.family == "windows"
34+
* @summary Verify behavior for file names with a trailing space
35+
* @run junit WinTrailingSpace
36+
*/
37+
public class WinTrailingSpace {
38+
private static final String FILENAME_NO_TRAILING_SPACE = "foobargus";
39+
private static final String FILENAME_TRAILING_SPACE = "foobargus ";
40+
private static final String DIRNAME_TRAILING_SPACE = "foo \\bar\\gus";
41+
42+
@Test
43+
public void noTrailingSpace() throws IOException {
44+
File f = null;
45+
try {
46+
f = new File(".", FILENAME_NO_TRAILING_SPACE);
47+
f.delete();
48+
f.createNewFile();
49+
assertTrue(f.exists());
50+
} finally {
51+
if (f != null)
52+
f.delete();
53+
}
54+
}
55+
56+
@Test
57+
public void trailingSpace() throws IOException {
58+
File f = null;
59+
try {
60+
f = new File(".", FILENAME_TRAILING_SPACE);
61+
f.delete();
62+
f.createNewFile();
63+
assertFalse(f.exists(), "Creation of " + f + " should have failed");
64+
} catch (IOException expected) {
65+
} finally {
66+
if (f != null)
67+
f.delete();
68+
}
69+
}
70+
71+
@Test
72+
public void dirTrailingSpace() throws IOException {
73+
File f = null;
74+
try {
75+
f = new File(".", DIRNAME_TRAILING_SPACE);
76+
f.delete();
77+
assertFalse(f.mkdirs(), "Creation of " + f + " should have failed");
78+
} finally {
79+
if (f != null)
80+
f.delete();
81+
}
82+
}
83+
}

0 commit comments

Comments
 (0)