Skip to content

Commit

Permalink
JBR-8235 AIOOBE from newByteChannel on empty path name
Browse files Browse the repository at this point in the history
  • Loading branch information
vladimirlagunov authored and mkartashev committed Feb 10, 2025
1 parent 12009ba commit f179770
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/java.base/unix/classes/sun/nio/fs/UnixChannelFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -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] == '/')))
{
Expand Down
16 changes: 15 additions & 1 deletion test/jdk/java/nio/file/Files/Misc.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -55,6 +56,7 @@ public static void main(String[] args) throws IOException {
testIsSameFile(dir);
testFileTypeMethods(dir);
testAccessMethods(dir);
testEmptyPathForByteStream();
} finally {
TestUtil.removeAll(dir);
}
Expand Down Expand Up @@ -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");
Expand Down

0 comments on commit f179770

Please sign in to comment.