From 8fd52796c41f85714a693b5462823004756aa8c9 Mon Sep 17 00:00:00 2001 From: Todd Kennedy Date: Wed, 10 Mar 2021 08:55:48 -0800 Subject: [PATCH] Null check before invoking callback Also made sure to invoke the callback on the main handler thread, like all other callbacks. Fixes: 182164620 Test: atest PackageManagerTests Change-Id: Ic2abe0877a716e83a10965bf348888a53ae95db8 --- .../app/ApplicationPackageManager.java | 19 ++++++++++--------- .../content/pm/PackageManagerTests.java | 9 ++++++++- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/core/java/android/app/ApplicationPackageManager.java b/core/java/android/app/ApplicationPackageManager.java index a6260d6d9cadd..dba62b9d3b63d 100644 --- a/core/java/android/app/ApplicationPackageManager.java +++ b/core/java/android/app/ApplicationPackageManager.java @@ -3228,6 +3228,12 @@ public class ApplicationPackageManager extends PackageManager { @Override public void registerDexModule(@NonNull String dexModule, @Nullable DexModuleRegisterCallback callback) { + // Create the callback delegate to be passed to package manager service. + DexModuleRegisterCallbackDelegate callbackDelegate = null; + if (callback != null) { + callbackDelegate = new DexModuleRegisterCallbackDelegate(callback); + } + // Check if this is a shared module by looking if the others can read it. boolean isSharedModule = false; try { @@ -3236,18 +3242,13 @@ public class ApplicationPackageManager extends PackageManager { isSharedModule = true; } } catch (ErrnoException e) { - callback.onDexModuleRegistered(dexModule, false, - "Could not get stat the module file: " + e.getMessage()); + if (callbackDelegate != null) { + callback.onDexModuleRegistered(dexModule, false, + "Could not get stat the module file: " + e.getMessage()); + } return; } - // Module path is ok. - // Create the callback delegate to be passed to package manager service. - DexModuleRegisterCallbackDelegate callbackDelegate = null; - if (callback != null) { - callbackDelegate = new DexModuleRegisterCallbackDelegate(callback); - } - // Invoke the package manager service. try { mPM.registerDexModule(mContext.getPackageName(), dexModule, diff --git a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java index 57c0be5007b87..5d75d9b3f70b5 100644 --- a/core/tests/coretests/src/android/content/pm/PackageManagerTests.java +++ b/core/tests/coretests/src/android/content/pm/PackageManagerTests.java @@ -2923,7 +2923,7 @@ public class PackageManagerTests extends AndroidTestCase { assertFalse("BaseCodePath should not be registered", callback.mSuccess); } - // Verify thatmodules which are not own by the calling package are not registered. + // Verify that modules which are not own by the calling package are not registered. public void testRegisterDexModuleNotOwningModule() throws Exception { TestDexModuleRegisterCallback callback = new TestDexModuleRegisterCallback(); String moduleBelongingToOtherPackage = "/data/user/0/com.google.android.gms/module.apk"; @@ -2978,6 +2978,13 @@ public class PackageManagerTests extends AndroidTestCase { assertFalse("DexModule registration should fail", callback.mSuccess); } + // If the module does not exist on disk we should get a failure. + public void testRegisterDexModuleNotExistsNoCallback() throws Exception { + ApplicationInfo info = getContext().getApplicationInfo(); + String nonExistentApk = Paths.get(info.dataDir, "non-existent.apk").toString(); + getPm().registerDexModule(nonExistentApk, null); + } + // Copied from com.android.server.pm.InstructionSets because we don't have access to it here. private static String[] getAppDexInstructionSets(ApplicationInfo info) { if (info.primaryCpuAbi != null) {