Null check on permission request check

This change adds a null check on PackageSetting.pkg before passing it on
to the method that checks for the QUERY_ALL_PACKAGES permission on the
package.

Test: atest AppEnumerationTests AppsFilterTest
Bug: 150405193
Change-Id: Ic92e29c685ff215f1c54dbb4aa8a0c6377a30fa7
diff --git a/services/core/java/com/android/server/pm/AppsFilter.java b/services/core/java/com/android/server/pm/AppsFilter.java
index 85c104c..ccda587 100644
--- a/services/core/java/com/android/server/pm/AppsFilter.java
+++ b/services/core/java/com/android/server/pm/AppsFilter.java
@@ -964,12 +964,14 @@
             try {
                 Trace.traceBegin(TRACE_TAG_PACKAGE_MANAGER, "requestsQueryAllPackages");
                 if (callingPkgSetting != null) {
-                    if (requestsQueryAllPackages(callingPkgSetting)) {
-                        return false;
-                    }
+                        if (callingPkgSetting.pkg != null
+                                && requestsQueryAllPackages(callingPkgSetting.pkg)) {
+                            return false;
+                        }
                 } else {
                     for (int i = callingSharedPkgSettings.size() - 1; i >= 0; i--) {
-                        if (requestsQueryAllPackages(callingSharedPkgSettings.valueAt(i))) {
+                        AndroidPackage pkg = callingSharedPkgSettings.valueAt(i).pkg;
+                        if (pkg != null && requestsQueryAllPackages(pkg)) {
                             return false;
                         }
                     }
@@ -1058,10 +1060,10 @@
     }
 
 
-    private static boolean requestsQueryAllPackages(PackageSetting pkgSetting) {
+    private static boolean requestsQueryAllPackages(@NonNull AndroidPackage pkg) {
         // we're not guaranteed to have permissions yet analyzed at package add, so we inspect the
         // package directly
-        return pkgSetting.pkg.getRequestedPermissions().contains(
+        return pkg.getRequestedPermissions().contains(
                 Manifest.permission.QUERY_ALL_PACKAGES);
     }