Skip to content

8361587: AssertionError in File.listFiles() when path is empty and -esa is enabled #26224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 10 commits into from
1 change: 0 additions & 1 deletion src/java.base/share/classes/java/io/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,6 @@ private File(String pathname, int prefixLength) {
*/
private File(String child, File parent) {
assert parent.path != null;
assert (!parent.path.isEmpty());
this.path = FS.resolve(parent.path, child);
this.prefixLength = parent.prefixLength;
}
Expand Down
1 change: 1 addition & 0 deletions src/java.base/unix/classes/java/io/UnixFileSystem.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ private static String trimSeparator(String s) {
@Override
public String resolve(String parent, String child) {
if (child.isEmpty()) return parent;
if (parent.isEmpty()) return child;
if (child.charAt(0) == '/') {
if (parent.equals("/")) return child;
return trimSeparator(parent + child);
Expand Down
77 changes: 76 additions & 1 deletion test/jdk/java/io/File/EmptyPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
*/

/* @test
* @bug 4842706 8024695
* @bug 4842706 8024695 8361587
* @summary Test some file operations with empty path
* @run junit EmptyPath
*/
Expand All @@ -36,7 +36,9 @@
import java.nio.file.Path;
import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -105,13 +107,28 @@ public void exists() {
assertTrue(f.exists());
}

@Test
public void getAbsoluteFile() {
assertEquals(p.toAbsolutePath().toFile(), f.getAbsoluteFile());
}

@Test
public void getAbsolutePath() {
System.out.println(p.toAbsolutePath().toString() + "\n" +
f.getAbsolutePath());
assertEquals(p.toAbsolutePath().toString(), f.getAbsolutePath());
}

@Test
public void getCanonicalFile() throws IOException {
assertEquals(p.toRealPath().toFile(), f.getCanonicalFile());
}

@Test
public void getCanonicalPath() throws IOException {
assertEquals(p.toRealPath().toString(), f.getCanonicalPath());
}

private void checkSpace(long expected, long actual) {
if (expected == 0) {
assertEquals(0L, actual);
Expand All @@ -136,6 +153,11 @@ public void getParent() {
assertNull(f.getParent());
}

@Test
public void getParentFile() {
assertNull(f.getParentFile());
}

@Test
public void getPath() {
assertEquals(p.toString(), f.getPath());
Expand Down Expand Up @@ -198,11 +220,58 @@ public void list() throws IOException {
assertEquals(nioSet, ioSet);
}

@Test
public void listFiles() throws IOException {
File child = new File(f.getAbsoluteFile(), "child");
assertTrue(child.createNewFile());
child.deleteOnExit();

File[] files = f.listFiles();
for (File file : files)
assertEquals(-1, f.toString().indexOf(File.separatorChar));

Set<String> ioSet = Arrays.stream(files)
.map(File::getName)
.collect(Collectors.toSet());

assertTrue(ioSet.contains(child.getName()));

Set<String> nioSet = Files.list(p)
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toSet());
assertEquals(nioSet, ioSet);
}

@Test
public void listRoots() {
Set<String> expected = Arrays.stream(f.getAbsoluteFile().listRoots())
.map(File::toString)
.collect(Collectors.toSet());
Set<String> actual = Arrays.stream(f.listRoots())
.map(File::toString)
.collect(Collectors.toSet());
assertEquals(expected, actual);
}

@Test
public void mkdir() {
assertFalse(f.mkdir());
}

@Test
public void mkdirs() {
File child = new File(f, "child");
assertFalse(child.mkdirs());
}

@Test
public void renameTo() throws IOException {
File tmp = File.createTempFile("foo", "bar", f.getAbsoluteFile());
assertTrue(tmp.exists());
assertFalse(f.renameTo(tmp));
}

@Test
public void setLastModified() {
long t0 = f.lastModified();
Expand Down Expand Up @@ -271,6 +340,12 @@ public void toPath() {
assertEquals(p, f.toPath());
}

@Test
public String toString() {
assertEquals(EMPTY_STRING, f.toString());
return EMPTY_STRING;
}

@Test
public void toURI() {
assertEquals(f.toPath().toUri(), f.toURI());
Expand Down