Skip to content

Commit 9b0b1f2

Browse files
committed
avoid NPE in to AbsolutePath and toRealPath if file applies to extract filter but does not exits
1 parent b2cef0f commit 9b0b1f2

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

graalpython/com.oracle.graal.python.test.integration/src/org/graalvm/python/embedding/utils/test/VirtualFileSystemTest.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ public void toRealPath() throws Exception {
151151
assertEquals(VFS_MOUNT_POINT + File.separator + "SomeFile", fs.toRealPath(Path.of(VFS_MOUNT_POINT + File.separator + "SomeFile")).toString());
152152
// check to be extracted file
153153
checkExtractedFile(fs.toRealPath(Path.of(VFS_MOUNT_POINT + File.separator + "extractme")), new String[]{"text1", "text2"});
154+
assertEquals(VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme", fs.toRealPath(Path.of(VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme")).toString());
154155
}
155156

156157
// from real FS
@@ -173,6 +174,7 @@ public void toAbsolutePath() throws Exception {
173174
// check to be extracted file
174175
checkExtractedFile(fs.toAbsolutePath(Path.of(VFS_MOUNT_POINT + File.separator + "extractme")), new String[]{"text1", "text2"});
175176
checkExtractedFile(fs.toAbsolutePath(Path.of(VFS_MOUNT_POINT + File.separator + "dir1/extractme")), null);
177+
assertEquals(VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme", fs.toAbsolutePath(Path.of(VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme")).toString());
176178
}
177179

178180
// from real FS
@@ -213,6 +215,9 @@ public void parseStringPath(BiFunction<FileSystem, String, Path> parsePath) thro
213215
p = parsePath.apply(fs, VFS_MOUNT_POINT + File.separator + "dir1/extractme");
214216
assertFalse(Files.exists(p));
215217
assertEquals(Path.of(VFS_MOUNT_POINT + File.separator + "dir1/extractme"), p);
218+
p = parsePath.apply(fs, VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme");
219+
assertFalse(Files.exists(p));
220+
assertEquals(Path.of(VFS_MOUNT_POINT + File.separator + "does-not-exist/extractme"), p);
216221
}
217222

218223
// from real FS
@@ -260,6 +265,13 @@ public void checkAccess() throws IOException {
260265
return null;
261266
},
262267
"should not be able to access a file which does not exist in VFS");
268+
checkException(
269+
NoSuchFileException.class,
270+
() -> {
271+
fs.checkAccess(Path.of(VFS_MOUNT_POINT + File.separator + "does-not-exits/extractme"), Set.of(AccessMode.READ));
272+
return null;
273+
},
274+
"should not be able to access a file which does not exist in VFS");
263275
}
264276

265277
// from real FS

graalpython/org.graalvm.python.embedding/src/org/graalvm/python/embedding/utils/VirtualFileSystem.java

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -595,7 +595,8 @@ private Path extractPath(Path path, boolean extractLibsDir) {
595595
*/
596596
BaseEntry entry = getEntry(path);
597597
if (entry == null) {
598-
severe("no entry for '%s'", path);
598+
warn("no entry for '%s'", path);
599+
return null;
599600
}
600601
Path relPath = mountPoint.relativize(Paths.get(entry.getPlatformPath()));
601602

@@ -968,6 +969,10 @@ public Path toAbsolutePath(Path path) {
968969
Path result = path;
969970
if (pathIsInVFS && shouldExtract(path)) {
970971
result = getExtractedPath(path);
972+
if (result == null) {
973+
finer("VFS.toAbsolutePath could not extract '%s'", path);
974+
result = path;
975+
}
971976
}
972977
Path ret = toAbsolutePathInternal(result);
973978
finer("VFS.toAbsolutePath '%s' -> '%s'", path, ret);
@@ -992,6 +997,10 @@ public Path toRealPath(Path path, LinkOption... linkOptions) throws IOException
992997
Path result = path;
993998
if (pathIsInVFS && shouldExtract(path)) {
994999
result = getExtractedPath(path);
1000+
if (result == null) {
1001+
finer("VFS.toRealPath could not extract '%s'", path);
1002+
result = path;
1003+
}
9951004
}
9961005
Path ret = result.normalize();
9971006
finer("VFS.toRealPath '%s' -> '%s'", path, ret);
@@ -1048,12 +1057,6 @@ private static void warn(String msgFormat, Object... args) {
10481057
}
10491058
}
10501059

1051-
private static void info(String msgFormat, Object... args) {
1052-
if (LOGGER.isLoggable(Level.INFO)) {
1053-
LOGGER.log(Level.INFO, String.format(msgFormat, args));
1054-
}
1055-
}
1056-
10571060
private static void fine(String msgFormat, Object... args) {
10581061
if (LOGGER.isLoggable(Level.FINE)) {
10591062
LOGGER.log(Level.FINE, String.format(msgFormat, args));
@@ -1072,9 +1075,4 @@ private static void finest(String msgFormat, Object... args) {
10721075
}
10731076
}
10741077

1075-
private static void severe(String msgFormat, Object... args) {
1076-
if (LOGGER.isLoggable(Level.SEVERE)) {
1077-
LOGGER.log(Level.SEVERE, String.format(msgFormat, args));
1078-
}
1079-
}
10801078
}

0 commit comments

Comments
 (0)