Skip to content

Commit fcc2621

Browse files
committed
[GR-28319] Ignore non zip/jar files in classpath #3071.
PullRequest: graal/7965
2 parents 738f09c + 5ee3bc3 commit fcc2621

File tree

3 files changed

+32
-23
lines changed

3 files changed

+32
-23
lines changed

substratevm/src/com.oracle.svm.core/src/com/oracle/svm/core/util/ClasspathUtils.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
package com.oracle.svm.core.util;
2626

2727
import java.io.File;
28+
import java.io.IOException;
29+
import java.io.RandomAccessFile;
2830
import java.nio.file.Files;
2931
import java.nio.file.Path;
3032
import java.nio.file.Paths;
@@ -63,6 +65,15 @@ public static String classpathToString(Path cp) {
6365
public static boolean isJar(Path p) {
6466
Path fn = p.getFileName();
6567
assert fn != null;
66-
return fn.toString().toLowerCase().endsWith(".jar") && Files.isRegularFile(p);
68+
if (Files.exists(p) && Files.isRegularFile(p) && Files.isReadable(p)) {
69+
try (RandomAccessFile file = new RandomAccessFile(p.toFile(), "r")) {
70+
final int magic = file.readInt();
71+
return magic == 0x504B0304 || magic == 0x504B0506 || magic == 0x504B0708;
72+
} catch (IOException e) {
73+
return false;
74+
}
75+
}
76+
77+
return false;
6778
}
6879
}

substratevm/src/com.oracle.svm.driver/src/com/oracle/svm/driver/NativeImage.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ private void processClasspathNativeImageMetaInf(Path classpathEntry, NativeImage
900900
} catch (NoSuchFileException e) {
901901
/* Fallthrough */
902902
}
903-
} else if (Files.isReadable(classpathEntry)) {
903+
} else if (ClasspathUtils.isJar(classpathEntry)) {
904904
jarFileMatches = Collections.singletonList(classpathEntry);
905905
}
906906

@@ -987,7 +987,7 @@ static void processManifestMainAttributes(Path path, BiConsumer<Path, Attributes
987987
} catch (IOException e) {
988988
throw NativeImage.showError("Error while expanding wildcard for '" + path + "'", e);
989989
}
990-
} else if (!Files.isDirectory(path)) {
990+
} else if (ClasspathUtils.isJar(path)) {
991991
processJarManifestMainAttributes(path, manifestConsumer);
992992
}
993993
}

substratevm/src/com.oracle.svm.hosted/src/com/oracle/svm/hosted/AbstractNativeImageClassLoaderSupport.java

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -176,30 +176,28 @@ private static Set<Path> getExcludeDirectories() {
176176
}
177177

178178
private void loadClassesFromPath(Path path) {
179-
if (Files.exists(path)) {
180-
if (Files.isRegularFile(path)) {
179+
if (ClasspathUtils.isJar(path)) {
180+
try {
181+
URI jarURI = new URI("jar:" + path.toAbsolutePath().toUri());
182+
FileSystem probeJarFileSystem;
181183
try {
182-
URI jarURI = new URI("jar:" + path.toAbsolutePath().toUri());
183-
FileSystem probeJarFileSystem;
184-
try {
185-
probeJarFileSystem = FileSystems.newFileSystem(jarURI, Collections.emptyMap());
186-
} catch (UnsupportedOperationException e) {
187-
/* Silently ignore invalid jar-files on image-classpath */
188-
probeJarFileSystem = null;
189-
}
190-
if (probeJarFileSystem != null) {
191-
try (FileSystem jarFileSystem = probeJarFileSystem) {
192-
loadClassesFromPath(jarFileSystem.getPath("/"), Collections.emptySet());
193-
}
184+
probeJarFileSystem = FileSystems.newFileSystem(jarURI, Collections.emptyMap());
185+
} catch (UnsupportedOperationException e) {
186+
/* Silently ignore invalid jar-files on image-classpath */
187+
probeJarFileSystem = null;
188+
}
189+
if (probeJarFileSystem != null) {
190+
try (FileSystem jarFileSystem = probeJarFileSystem) {
191+
loadClassesFromPath(jarFileSystem.getPath("/"), Collections.emptySet());
194192
}
195-
} catch (ClosedByInterruptException ignored) {
196-
throw new InterruptImageBuilding();
197-
} catch (IOException | URISyntaxException e) {
198-
throw shouldNotReachHere(e);
199193
}
200-
} else {
201-
loadClassesFromPath(path, excludeDirectories);
194+
} catch (ClosedByInterruptException ignored) {
195+
throw new InterruptImageBuilding();
196+
} catch (IOException | URISyntaxException e) {
197+
throw shouldNotReachHere(e);
202198
}
199+
} else {
200+
loadClassesFromPath(path, excludeDirectories);
203201
}
204202
}
205203

0 commit comments

Comments
 (0)