Ghidra is a software reverse engineering (SRE) framework
GP-3364: Preventing NPE's in OSGi code
Ryan Kurtze6694716d3af
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.javaindex 72e73a02cf..f760055fb9 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleHost.java@@ -457,6 +457,10 @@ public class BundleHost {frameworkBundleContext = felixFramework.getBundleContext();+ if (frameworkBundleContext == null) {+ throw new OSGiException("Felix OSGi framework has no bundle context");+ }+addDebuggingListeners();Bundle bundle = frameworkBundleContext.getBundle();@@ -512,6 +516,11 @@ public class BundleHost {* @return the OSGi bundle or null*/Bundle getOSGiBundle(String bundleLocation) {+ // TODO: Is it safe/better to return null when the framework isn't started?+ // 'frameworkBundleContext' is currently not set to null when it gets stopped.+ if (frameworkBundleContext == null) {+ return null;+ }return frameworkBundleContext.getBundle(bundleLocation);}@@ -667,7 +676,7 @@ public class BundleHost {}catch (Exception e) {// write the error to the console or log file- console.println("Unexpected error activating bundles: " + bundles);+ console.println("Unexpected error activating bundle: " + bundle);e.printStackTrace(console);}monitor.incrementProgress(1);@@ -736,7 +745,7 @@ public class BundleHost {}catch (Exception e) {// write the error to the console or log file- console.println("Unexpected error activating bundles: " + bundles);+ console.println("Unexpected error activating bundle: " + bundle);e.printStackTrace(console);}monitor.incrementProgress(1);diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.javaindex 492ec60b9a..0ee45578e1 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/GhidraBundle.java@@ -211,6 +211,7 @@ public abstract class GhidraBundle {@Overridepublic String toString() {- return getOSGiBundle().getSymbolicName();+ Bundle bundle = getOSGiBundle();+ return bundle != null ? bundle.getSymbolicName() : bundleFile.toString();}}diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.javaindex a8a9c5bc83..01d18ec87f 100644--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java@@ -148,7 +148,12 @@ public class JavaScriptProvider extends GhidraScriptProvider {bundleHost.activateAll(Collections.singletonList(bundle), TaskMonitor.DUMMY, writer);String classname = bundle.classNameForScript(sourceFile);- Class<?> clazz = bundle.getOSGiBundle().loadClass(classname); // throws ClassNotFoundException+ Bundle osgiBundle = bundle.getOSGiBundle();+ if (osgiBundle == null) {+ throw new ClassNotFoundException(+ "Failed to get OSGi bundle containing script: " + sourceFile.toString());+ }+ Class<?> clazz = osgiBundle.loadClass(classname); // throws ClassNotFoundExceptionreturn clazz;}