Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(android, utils): fix rare crash getting documents directory #5118

Merged
merged 2 commits into from
Apr 9, 2021

Conversation

LittoCats
Copy link
Contributor

@LittoCats LittoCats commented Apr 3, 2021

NullPointerException Firebase Crashlytics

Description

The exported constant value for key KEY_DOCUMENT_DIRECTORY will be missed in ReactNativeFirebaseUtilsModule when Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT and externalDirectory == null

Related issues

Fixes #4706

Release Summary

Checklist

  • I read the Contributor Guide and followed the process outlined there for submitting PRs.
    • Yes
  • My change supports the following platforms;
    • Android
    • iOS
  • My change includes tests;
    • e2e tests added or updated in packages/\*\*/e2e
    • jest tests added or updated in packages/\*\*/__tests__
  • I have updated TypeScript types that are affected by my change.
  • This is a breaking change;
    • Yes
    • No

Test Plan


Think react-native-firebase is great? Please consider supporting the project with any of the below:

@vercel
Copy link

vercel bot commented Apr 3, 2021

This pull request is being automatically deployed with Vercel (learn more).
To see the status of your deployment, click below or on the icon next to each commit.

🔍 Inspect: https://vercel.com/invertase/react-native-firebase/GuHm9FZ3bJhW76Q9L8k7QqNzb9pb
✅ Preview: https://react-native-firebase-git-fork-littocats-crash-fix-invertase.vercel.app

@CLAassistant
Copy link

CLAassistant commented Apr 3, 2021

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you all sign our Contributor License Agreement before we can accept your contribution.
1 out of 2 committers have signed the CLA.

✅ mikehardy
❌ LittoCats
You have signed the CLA already but the status is still pending? Let us recheck it.

@codecov
Copy link

codecov bot commented Apr 3, 2021

Codecov Report

Merging #5118 (ea8e36f) into master (d2838ff) will not change coverage.
The diff coverage is n/a.

❗ Current head ea8e36f differs from pull request most recent head 1146880. Consider uploading reports for the commit 1146880 to get more accurate results

@@           Coverage Diff           @@
##           master    #5118   +/-   ##
=======================================
  Coverage   88.99%   88.99%           
=======================================
  Files         109      109           
  Lines        3739     3739           
  Branches      358      358           
=======================================
  Hits         3327     3327           
  Misses        367      367           
  Partials       45       45           

@mikehardy
Copy link
Collaborator

Hi @LittoCats ! I think this was already addressed in #4990 - when I look at the original diagnosis you did it seems correct but it's based on an old version of the code. In v11.1.0+ It already has a null check and so will not crash here

@mikehardy mikehardy added the Workflow: Waiting for User Response Blocked waiting for user response. label Apr 3, 2021
@LittoCats
Copy link
Contributor Author

Yes, I notice that last night, but I found, the constant value exported to js will be not exists for KEY_DOCUMENT_DIRECTORY when Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT and externalDirectory == null

    File externalDirectory = context.getExternalFilesDir(null);
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
      if ( externalDirectory != null ) {
        constants.put(KEY_DOCUMENT_DIRECTORY, externalDirectory.getAbsolutePath());
      }
    } else {
      constants.put(KEY_DOCUMENT_DIRECTORY, context.getFilesDir().getAbsolutePath());
    }

@mikehardy
Copy link
Collaborator

That's true and I thought of that as I compared the two types of fix for the issue. Your fix will cause the directory to change between runs though, so the value will exist in your version but it won't be stable as external media is changed.

I think that may just be an unsolvable problem though - maybe something just to put in the docs here vs attempt to solve in code

The doc is here

* Use this directory to place documents that have been created by the user.
and if there was just a note that there is a small chance if a user uses removable external storage on Android and that storage configuration changes or is not available, the path returned could change, then this seems fine.

@LittoCats
Copy link
Contributor Author

Thanks for your explaination.

@LittoCats LittoCats closed this Apr 6, 2021
@mikehardy mikehardy reopened this Apr 6, 2021
@mikehardy
Copy link
Collaborator

Wait! @LittoCats I actually like this PR, I think the code itself is both correct and necessary to correct the null pointer exception - I am sorry I wasn't clear, what I think it needs is just an addition to the documentation as well as the code fix, to warn developers that this is an external directory which could change (making it appear as if documents appear or disappear) if the users external storage environment changes

Hopefully that's more clear :-). I actually really want to get this fix in, you have solved a real crash!

LittoCats and others added 2 commits April 8, 2021 17:54
….SDK_INT >= Build.VERSION_CODES.KITKAT and externalDirectory != null
this ensures the documents directory always exists, at the expense
of having it be a different value under rare circumstances (better
than crashing at least...)

Docs updated to reflect the rare cases it can return unexpected values
@mikehardy mikehardy changed the title Fix: NullPointerException Firebase Crashlytics fix(android, utils): fix rare crash getting documents directory Apr 8, 2021
@mikehardy
Copy link
Collaborator

@LittoCats I had time today to look at this - I pulled your branch, fixed the compile error and then altered the documentation to explain the conditions when documents directory could be internal vs external.

I think this should work and fix the crash - can you take a look? I can merge it and release it if it looks good

@mikehardy mikehardy added the Workflow: Pending Merge Waiting on CI or similar label Apr 8, 2021
@mikehardy mikehardy merged commit f0a2957 into invertase:master Apr 9, 2021
@mikehardy mikehardy removed Workflow: Pending Merge Waiting on CI or similar Workflow: Waiting for User Response Blocked waiting for user response. labels Apr 9, 2021
androidIsForVivek pushed a commit to androidIsForVivek/react-native-firebase that referenced this pull request Aug 9, 2021
…rtase#5118)

* Fix: value for key KEY_DOCUMENT_DIRECTORY not exists if Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT and externalDirectory != null

* fix(android, utils): fix rare crash getting documents directory

this ensures the documents directory always exists, at the expense
of having it be a different value under rare circumstances (better
than crashing at least...)

Docs updated to reflect the rare cases it can return unexpected values

Co-authored-by: Mike Hardy <github@mikehardy.net>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[🐛🔥] NullPointerException Firebase Crashlytics
3 participants