Skip to content

Commit

Permalink
fix(android): Prevent ArrayIndexOutOfBoundsException in permission ch…
Browse files Browse the repository at this point in the history
…eck (#47047)

Summary:
This PR addresses a potential `ArrayIndexOutOfBoundsException` in the Android module's permission checking logic.

```diff
- results.length > 0 && results[j] == PackageManager.PERMISSION_GRANTED
+ results.length > j && results[j] == PackageManager.PERMISSION_GRANTED
```

It ensures that we only access the results array when the index `j` is within bounds, preventing crashes due to invalid array access that have been occurring in the production environment.

Here is the Crashlytics dashboard concerning this type of crash on my app last week (react-native 0.75.4 - old arch):
![image](https://github.com/user-attachments/assets/0b7c1a10-39f6-4a17-9eee-4b17986f5b85)

## Changelog:

[ANDROID] [FIXED] - Prevent ArrayIndexOutOfBoundsException in permission check

Pull Request resolved: #47047

Test Plan:
1. Verify normal permission request scenarios still work on Android:
   - Request a permission (e.g., camera, location)
   - Grant the permission
   - Verify the app functions correctly with the granted permission

2. Monitor production crash reports:
   - Deploy the fix to production
   - Observe a decrease in ArrayIndexOutOfBoundsException occurrences in the permission check logic

Note: As this crash was only observed in production, we'll rely on production monitoring to verify the fix's effectiveness over time.

Reviewed By: rshest

Differential Revision: D64464171

Pulled By: cortinico

fbshipit-source-id: 87beacf7ee749a51665577d05a56cc8d6889d70f
  • Loading branch information
antFrancon authored and facebook-github-bot committed Oct 16, 2024
1 parent 6b67772 commit 6aeca53
Showing 1 changed file with 1 addition and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public class PermissionsModule(reactContext: ReactApplicationContext?) :
val callbackActivity = args[1] as PermissionAwareActivity
for (j in permissionsToCheck.indices) {
val permission = permissionsToCheck[j]
if (results.size > 0 && results[j] == PackageManager.PERMISSION_GRANTED) {
if (results.size > j && results[j] == PackageManager.PERMISSION_GRANTED) {
grantedPermissions.putString(permission, GRANTED)
} else {
if (callbackActivity.shouldShowRequestPermissionRationale(permission)) {
Expand Down

0 comments on commit 6aeca53

Please sign in to comment.