From 7bf8cd085a19297523eb0fc74c00cee8c6934d49 Mon Sep 17 00:00:00 2001 From: Peter Shipton Date: Wed, 23 Sep 2020 00:02:01 -0400 Subject: [PATCH] Fix possible NPE in J9VMInternals.getClassInfoStrings() Signed-off-by: Peter Shipton --- .../classes/java/lang/J9VMInternals.java | 51 ++++++++++++------- 1 file changed, 32 insertions(+), 19 deletions(-) diff --git a/jcl/src/java.base/share/classes/java/lang/J9VMInternals.java b/jcl/src/java.base/share/classes/java/lang/J9VMInternals.java index 1a466c34f4d..764d683c888 100644 --- a/jcl/src/java.base/share/classes/java/lang/J9VMInternals.java +++ b/jcl/src/java.base/share/classes/java/lang/J9VMInternals.java @@ -34,13 +34,12 @@ import java.util.Properties; import java.util.WeakHashMap; import java.security.AccessControlContext; +import java.security.CodeSource; import java.security.ProtectionDomain; import java.io.InputStream; import java.io.OutputStream; import java.io.PrintStream; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.lang.reflect.Proxy; +import java.net.URL; /*[IF Sidecar19-SE]*/ import jdk.internal.ref.CleanerShutdown; import jdk.internal.ref.CleanerImpl; @@ -492,25 +491,39 @@ static final class ClassInitializationLock { private static String[] getClassInfoStrings(final Class clazz, String classPath){ - String classLoader; + String classLoaderStr = ""; //$NON-NLS-1$ + String cpResult = ""; //$NON-NLS-1$ - if (classPath != null) { - classLoader = ""; - } else { - classLoader = clazz.getClassLoader().toString(); - classPath = (String)AccessController.doPrivileged(new PrivilegedAction() { - public Object run() { - String path = null; - try { - path = clazz.getProtectionDomain().getCodeSource().getLocation().toString(); - } catch (Exception e) { - path = ""; + if (classPath == null) { + ClassLoader classLoader = clazz.getClassLoader(); + if (classLoader != null) { + classLoaderStr = classLoader.toString(); + classPath = AccessController.doPrivileged(new PrivilegedAction() { + @Override + public String run() { + String path = null; + try { + ProtectionDomain pd = clazz.getProtectionDomain(); + if (pd != null) { + CodeSource cs = pd.getCodeSource(); + if (cs != null) { + URL loc = cs.getLocation(); + if (loc != null) { + path = loc.toString(); + } + } + } + } catch (Exception e) { + } + return path; } - return path; - } - }); + }); + } + } + if (classPath != null) { + cpResult = classPath; } - String [] strings = {classPath, classLoader}; + String [] strings = {cpResult, classLoaderStr}; return strings; }