8315273: (fs) Path.toRealPath(LinkOption.NOFOLLOW_LINKS) fails when "../../" follows a link (win)

Reviewed-by: djelinski
This commit is contained in:
Brian Burkhalter 2024-09-17 15:50:16 +00:00
parent b39e6a84ef
commit f87701635f
3 changed files with 24 additions and 6 deletions

View file

@ -803,8 +803,8 @@ public interface Path
* "{@code ..}" (or equivalent) is preceded by a non-"{@code ..}" name then * "{@code ..}" (or equivalent) is preceded by a non-"{@code ..}" name then
* an implementation will typically cause both names to be removed. When * an implementation will typically cause both names to be removed. When
* not resolving symbolic links and the preceding name is a symbolic link * not resolving symbolic links and the preceding name is a symbolic link
* then the names are only removed if it guaranteed that the resulting path * then the names are only removed if it is guaranteed that the resulting
* will locate the same file as this path. * path will locate the same file as this path.
* *
* @param options * @param options
* options indicating how symbolic links are handled * options indicating how symbolic links are handled

View file

@ -569,8 +569,6 @@ java/nio/channels/DatagramChannel/AfterDisconnect.java 8308807 aix-ppc6
java/nio/channels/DatagramChannel/ManySourcesAndTargets.java 8264385 macosx-aarch64 java/nio/channels/DatagramChannel/ManySourcesAndTargets.java 8264385 macosx-aarch64
java/nio/channels/DatagramChannel/Unref.java 8233437 generic-all java/nio/channels/DatagramChannel/Unref.java 8233437 generic-all
java/nio/file/Path/ToRealPath.java 8315273 windows-all
############################################################################ ############################################################################
# jdk_rmi # jdk_rmi

View file

@ -33,6 +33,8 @@ import java.io.IOException;
import java.io.UncheckedIOException; import java.io.UncheckedIOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.HashSet;
import java.util.Set;
import jdk.test.lib.Platform; import jdk.test.lib.Platform;
@ -51,6 +53,7 @@ public class ToRealPath {
static final Path SUBDIR; static final Path SUBDIR;
static final Path FILE; static final Path FILE;
static final Path LINK; static final Path LINK;
static final Set<Path> extraDeletions;
static { static {
try { try {
@ -59,6 +62,7 @@ public class ToRealPath {
FILE = Files.createFile(DIR.resolve("foo")); FILE = Files.createFile(DIR.resolve("foo"));
LINK = DIR.resolve("link"); LINK = DIR.resolve("link");
SUPPORTS_LINKS = TestUtil.supportsSymbolicLinks(DIR); SUPPORTS_LINKS = TestUtil.supportsSymbolicLinks(DIR);
extraDeletions = new HashSet<Path>();
} catch (IOException e) { } catch (IOException e) {
throw new UncheckedIOException(e); throw new UncheckedIOException(e);
} }
@ -154,7 +158,14 @@ public class ToRealPath {
System.out.println("p: " + p); System.out.println("p: " + p);
Path path = LINK.resolve(p); Path path = LINK.resolve(p);
System.out.println("path: " + path); System.out.println("path: " + path);
if (Platform.isWindows() && Files.notExists(path)) {
Files.createFile(path);
extraDeletions.add(path);
}
System.out.println("no follow: " + path.toRealPath(NOFOLLOW_LINKS)); System.out.println("no follow: " + path.toRealPath(NOFOLLOW_LINKS));
if (Platform.isWindows())
assertTrue(Files.isSameFile(path.toRealPath(NOFOLLOW_LINKS), path));
else
assertEquals(path.toRealPath(NOFOLLOW_LINKS), path); assertEquals(path.toRealPath(NOFOLLOW_LINKS), path);
Files.delete(sub); Files.delete(sub);
@ -177,7 +188,14 @@ public class ToRealPath {
Path p = Path.of("aaa", "..", "..", "bbb", "..", "..", "out.txt"); Path p = Path.of("aaa", "..", "..", "bbb", "..", "..", "out.txt");
Path path = DIR.resolve(p); Path path = DIR.resolve(p);
System.out.println("path: " + path); System.out.println("path: " + path);
if (Platform.isWindows() && Files.notExists(path)) {
Files.createFile(path);
extraDeletions.add(path);
}
System.out.println("no follow: " + path.toRealPath(NOFOLLOW_LINKS)); System.out.println("no follow: " + path.toRealPath(NOFOLLOW_LINKS));
if (Platform.isWindows())
assertTrue(Files.isSameFile(path.toRealPath(NOFOLLOW_LINKS), path));
else
assertEquals(path.toRealPath(NOFOLLOW_LINKS), path); assertEquals(path.toRealPath(NOFOLLOW_LINKS), path);
System.out.println(path.toRealPath()); System.out.println(path.toRealPath());
@ -235,5 +253,7 @@ public class ToRealPath {
Files.delete(FILE); Files.delete(FILE);
Files.delete(SUBDIR); Files.delete(SUBDIR);
Files.delete(DIR); Files.delete(DIR);
for (Path p : extraDeletions)
Files.deleteIfExists(p);
} }
} }