From f179770c52cc4e45c27a5e3c72045ae28d901429 Mon Sep 17 00:00:00 2001 From: Vladimir Lagunov Date: Wed, 5 Feb 2025 22:54:22 +0100 Subject: [PATCH] JBR-8235 AIOOBE from newByteChannel on empty path name --- .../classes/sun/nio/fs/UnixChannelFactory.java | 5 +++-- test/jdk/java/nio/file/Files/Misc.java | 16 +++++++++++++++- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java b/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java index dec13180f2f7..3076219d7156 100644 --- a/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java +++ b/src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java @@ -197,8 +197,9 @@ protected static FileDescriptor open(int dfd, if (flags.createNew) { byte[] pathForSysCall = path.asByteArray(); - // throw exception if file name is "." to avoid confusing error - if ((pathForSysCall[pathForSysCall.length-1] == '.') && + // throw exception if file name is "." or "" to avoid confusing error + if ((pathForSysCall.length == 0) || + (pathForSysCall[pathForSysCall.length-1] == '.') && (pathForSysCall.length == 1 || (pathForSysCall[pathForSysCall.length-2] == '/'))) { diff --git a/test/jdk/java/nio/file/Files/Misc.java b/test/jdk/java/nio/file/Files/Misc.java index 024b518141bb..e804cef3731c 100644 --- a/test/jdk/java/nio/file/Files/Misc.java +++ b/test/jdk/java/nio/file/Files/Misc.java @@ -30,16 +30,17 @@ */ import java.io.IOException; -import java.io.File; import java.nio.file.FileAlreadyExistsException; import java.nio.file.Files; import java.nio.file.Path; +import java.nio.file.StandardOpenOption; import java.nio.file.attribute.AclEntry; import java.nio.file.attribute.AclEntryPermission; import java.nio.file.attribute.AclEntryType; import java.nio.file.attribute.AclFileAttributeView; import java.nio.file.attribute.DosFileAttributeView; import java.nio.file.attribute.UserPrincipal; +import java.util.EnumSet; import java.util.List; import jdk.test.lib.Platform; @@ -55,6 +56,7 @@ public static void main(String[] args) throws IOException { testIsSameFile(dir); testFileTypeMethods(dir); testAccessMethods(dir); + testEmptyPathForByteStream(); } finally { TestUtil.removeAll(dir); } @@ -363,6 +365,18 @@ static void testAccessMethods(Path tmpdir) throws IOException { } } + static void testEmptyPathForByteStream() throws IOException { + Path emptyPath = Path.of(""); + try { + emptyPath.getFileSystem().provider() + .newByteChannel(emptyPath, EnumSet.of(StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW)) + .close(); + throw new RuntimeException("FileAlreadyExistsException was not thrown"); + } catch (FileAlreadyExistsException e) { + // The expected behavior. + } + } + static void assertTrue(boolean okay) { if (!okay) throw new RuntimeException("Assertion Failed");