Skip to content

Fixes AB#3553684 Fix NPE in PackageHelper.isPackageInstalledAndEnabled - #3051

Merged
Siddhi (siddhijain) merged 2 commits into
devfrom
fix/package-helper-npe-on-missing-package
Mar 24, 2026
Merged

Fixes AB#3553684 Fix NPE in PackageHelper.isPackageInstalledAndEnabled#3051
Siddhi (siddhijain) merged 2 commits into
devfrom
fix/package-helper-npe-on-missing-package

Conversation

@siddhijain

@siddhijain Siddhi (siddhijain) commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Fixes AB#3553684 When getApplicationInfo throws NameNotFoundException for an uninstalled package, the catch block logged the error but did not return. Execution fell through to code that could dereference applicationInfo.enabled, causing a NullPointerException.

Return false immediately in the NameNotFoundException catch block so the method exits cleanly when the queried package is not installed.

@siddhijain
Siddhi (siddhijain) requested a review from a team as a code owner March 24, 2026 03:47
@siddhijain Siddhi (siddhijain) added the No-Changelog This Pull-Request has no associated changelog entry. label Mar 24, 2026
Copilot AI review requested due to automatic review settings March 24, 2026 03:47
@github-actions

Copy link
Copy Markdown

✅ Work item link check complete. Description contains link AB#3553684 to an Azure Boards work item.

1 similar comment
@github-actions

Copy link
Copy Markdown

✅ Work item link check complete. Description contains link AB#3553684 to an Azure Boards work item.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Fixes a crash in PackageHelper.isPackageInstalledAndEnabled by ensuring the method exits immediately when PackageManager.getApplicationInfo() throws NameNotFoundException, preventing a null dereference path when checking ApplicationInfo.enabled.

Changes:

  • Add an early return false; inside the NameNotFoundException catch block in isPackageInstalledAndEnabled.

PackageManager.getApplicationInfo() can return null on some OEM devices
instead of throwing NameNotFoundException when a package is not installed.
The method then dereferences applicationInfo.enabled, causing:

  NullPointerException: Attempt to read from field
  'boolean android.content.pm.ApplicationInfo.enabled'
  on a null object reference

Add a null check on the ApplicationInfo result and return false
immediately if null. Also return false in the NameNotFoundException
catch block for completeness.

Add unit tests covering:
- NameNotFoundException (package not found)
- getApplicationInfo returns null (OEM behavior)
- Package installed and enabled
- Package installed but disabled
- USE_ENABLED_SETTING_FOR_PACKAGE_CHECK flight enabled path
- IllegalArgumentException fallback in flight-enabled path
@siddhijain
Siddhi (siddhijain) force-pushed the fix/package-helper-npe-on-missing-package branch from 6f5a451 to abf2627 Compare March 24, 2026 04:03

Copilot AI commented Mar 24, 2026

Copy link
Copy Markdown
Contributor

Siddhi (@siddhijain) I've opened a new pull request, #3052, to work on those changes. Once the pull request is ready, I'll request review from you.

@siddhijain
Siddhi (siddhijain) merged commit d5d744d into dev Mar 24, 2026
27 checks passed
Siddhi (siddhijain) added a commit that referenced this pull request Mar 24, 2026
#3051)

Fixes
[AB#3553684](https://identitydivision.visualstudio.com/fac9d424-53d2-45c0-91b5-ef6ba7a6bf26/_workitems/edit/3553684)
When getApplicationInfo throws NameNotFoundException for an uninstalled
package, the catch block logged the error but did not return. Execution
fell through to code that could dereference applicationInfo.enabled,
causing a NullPointerException.

Return false immediately in the NameNotFoundException catch block so the
method exits cleanly when the queried package is not installed.
Siddhi (siddhijain) added a commit that referenced this pull request Mar 24, 2026
#3053)

…d (#3051)

Fixes

[AB#3553684](https://identitydivision.visualstudio.com/fac9d424-53d2-45c0-91b5-ef6ba7a6bf26/_workitems/edit/3553684)
When getApplicationInfo throws NameNotFoundException for an uninstalled
package, the catch block logged the error but did not return. Execution
fell through to code that could dereference applicationInfo.enabled,
causing a NullPointerException.

Return false immediately in the NameNotFoundException catch block so the
method exits cleanly when the queried package is not installed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

No-Changelog This Pull-Request has no associated changelog entry.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants