Skip to content

fix: useLinking crash#10262

Merged
satya164 merged 2 commits into
react-navigation:mainfrom
roryabraham:roryabraham-useLinkingCrash
Jan 26, 2022
Merged

fix: useLinking crash#10262
satya164 merged 2 commits into
react-navigation:mainfrom
roryabraham:roryabraham-useLinkingCrash

Conversation

@roryabraham

Copy link
Copy Markdown
Contributor

Hello 👋

We've been seeing this crash (shown in the following screenshot) happening in our production code. I unfortunately don't know how to consistently reproduce this problem, but I believe this PR is very safe and will fix the issue.

image

As explained in this comment, TypeError: Cannot read property 'id' of undefined tells us that somehow the items array is actually getting in a state where it contains undefined.

Now, there's only four places the items array is written to: line 84, line 86, line 108, and line 110.

Reviewing these one-by-one:

Line 84

items = items.slice(0, index - 1) -> I don't see any way this can add an undefined value to the array ❌

Line 86

items.push({ path, state, id }) -> At worst, this could append an object like: {path: undefined, state: undefined, id: undefined}, but we wouldn't end up with undefined itself in the items array. So not here either ❌

Line 108

items = [{ path, state, id }]; -> Similar to line 86, this can't cause undefined to be in the items array ❌

Line 110

items[index] = { path, state, id }; -> This can cause undefined to be in the items array if index is greater than the length of the array, like so:

> let items = [];
undefined
> items[1] = 'Hello';
'Hello'
> items[0];
undefined

It seems this is our problem line, and tells us that the index is becoming greater than the length of the items array. I believe I found the only location in this component where that could happen, and fix it in this PR.

Let me know if you have questions or concerns! If anyone from the community wants to help out with providing testing or consistent reproduction steps, that would be great!

@github-actions

Copy link
Copy Markdown

Hey roryabraham! Thanks for opening your first pull request in this repo. If you haven't already, make sure to read our contribution guidelines.

@netlify

netlify Bot commented Jan 14, 2022

Copy link
Copy Markdown

✔️ Deploy Preview for react-navigation-example ready!

🔨 Explore the source changes: 66eca91

🔍 Inspect the deploy log: https://app.netlify.com/sites/react-navigation-example/deploys/61e5f8d2525f4a000743cc37

😎 Browse the preview: https://deploy-preview-10262--react-navigation-example.netlify.app/

@codecov-commenter

Copy link
Copy Markdown

Codecov Report

Merging #10262 (66eca91) into main (e9c6904) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##             main   #10262   +/-   ##
=======================================
  Coverage   74.41%   74.41%           
=======================================
  Files         160      160           
  Lines        4858     4855    -3     
  Branches     1838     1836    -2     
=======================================
- Hits         3615     3613    -2     
+ Misses       1209     1208    -1     
  Partials       34       34           
Impacted Files Coverage Δ
packages/native/src/useLinking.tsx 76.59% <100.00%> (+0.12%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update e9c6904...66eca91. Read the comment docs.

@satya164 satya164 merged commit e612a4c into react-navigation:main Jan 26, 2022
@hirbod

hirbod commented Jan 26, 2022

Copy link
Copy Markdown
Contributor

Yihaaa!

@satya164

Copy link
Copy Markdown
Member

Thank you for the PR and investigation

@github-actions

Copy link
Copy Markdown

Hey! This issue is closed and isn't watched by the core team. You are welcome to discuss the issue with others in this thread, but if you think this issue is still valid and needs to be tracked, please open a new issue with a repro.

MohamedLamineAllal pushed a commit to MohamedLamineAllal/react-navigation that referenced this pull request Feb 4, 2022
Hello 👋 

We've been seeing [this crash](react-navigation#9970 (comment)) (shown in the following screenshot) happening in our production code. I unfortunately don't know how to consistently reproduce this problem, but I believe this PR is very safe and will fix the issue.

![image](https://user-images.githubusercontent.com/47436092/149445563-ab5dbece-d3eb-4ca8-afac-fc2793d243c7.png)

As explained in [this comment](https://github.com/react-navigation/react-navigation/pull/9970/files#r784475821), `TypeError: Cannot read property 'id' of undefined` tells us that somehow the `items` array is actually getting in a state where it contains `undefined`.

Now, there's only four places the `items` array is written to: [line 84](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L84), [line 86](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L86), [line 108](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L108), and [line 110](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L110).

Reviewing these one-by-one:

#### Line 84
`items = items.slice(0, index - 1)` -> I don't see any way this can add an `undefined` value to the array ❌ 

#### Line 86
`items.push({ path, state, id })` -> At worst, this could append an object like: `{path: undefined, state: undefined, id: undefined}`, but we wouldn't end up with `undefined` itself in the `items` array. So not here either ❌ 

#### Line 108
`items = [{ path, state, id }];` -> Similar to line 86, this can't cause `undefined` to be in the `items` array ❌ 

#### Line 110
`items[index] = { path, state, id };` -> This _can_ cause `undefined` to be in the `items` array if `index` is greater than the length of the array, like so:

```js
> let items = [];
undefined
> items[1] = 'Hello';
'Hello'
> items[0];
undefined
```

It seems this is our problem line, and tells us that the `index` is becoming greater than the length of the items array. I believe I found the only location in this component where that could happen, and fix it in this PR.

Let me know if you have questions or concerns! If anyone from the community wants to help out with providing testing or consistent reproduction steps, that would be great!
MohamedLamineAllal pushed a commit to MohamedLamineAllal/react-navigation that referenced this pull request Feb 4, 2022
Hello 👋 

We've been seeing [this crash](react-navigation#9970 (comment)) (shown in the following screenshot) happening in our production code. I unfortunately don't know how to consistently reproduce this problem, but I believe this PR is very safe and will fix the issue.

![image](https://user-images.githubusercontent.com/47436092/149445563-ab5dbece-d3eb-4ca8-afac-fc2793d243c7.png)

As explained in [this comment](https://github.com/react-navigation/react-navigation/pull/9970/files#r784475821), `TypeError: Cannot read property 'id' of undefined` tells us that somehow the `items` array is actually getting in a state where it contains `undefined`.

Now, there's only four places the `items` array is written to: [line 84](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L84), [line 86](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L86), [line 108](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L108), and [line 110](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L110).

Reviewing these one-by-one:

#### Line 84
`items = items.slice(0, index - 1)` -> I don't see any way this can add an `undefined` value to the array ❌ 

#### Line 86
`items.push({ path, state, id })` -> At worst, this could append an object like: `{path: undefined, state: undefined, id: undefined}`, but we wouldn't end up with `undefined` itself in the `items` array. So not here either ❌ 

#### Line 108
`items = [{ path, state, id }];` -> Similar to line 86, this can't cause `undefined` to be in the `items` array ❌ 

#### Line 110
`items[index] = { path, state, id };` -> This _can_ cause `undefined` to be in the `items` array if `index` is greater than the length of the array, like so:

```js
> let items = [];
undefined
> items[1] = 'Hello';
'Hello'
> items[0];
undefined
```

It seems this is our problem line, and tells us that the `index` is becoming greater than the length of the items array. I believe I found the only location in this component where that could happen, and fix it in this PR.

Let me know if you have questions or concerns! If anyone from the community wants to help out with providing testing or consistent reproduction steps, that would be great!
MohamedLamineAllal pushed a commit to MohamedLamineAllal/react-navigation that referenced this pull request Feb 4, 2022
Hello 👋 

We've been seeing [this crash](react-navigation#9970 (comment)) (shown in the following screenshot) happening in our production code. I unfortunately don't know how to consistently reproduce this problem, but I believe this PR is very safe and will fix the issue.

![image](https://user-images.githubusercontent.com/47436092/149445563-ab5dbece-d3eb-4ca8-afac-fc2793d243c7.png)

As explained in [this comment](https://github.com/react-navigation/react-navigation/pull/9970/files#r784475821), `TypeError: Cannot read property 'id' of undefined` tells us that somehow the `items` array is actually getting in a state where it contains `undefined`.

Now, there's only four places the `items` array is written to: [line 84](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L84), [line 86](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L86), [line 108](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L108), and [line 110](https://github.com/react-navigation/react-navigation/blob/fb84805c889bbb7059e7e95592c004aea2a510d6/packages/native/src/useLinking.tsx#L110).

Reviewing these one-by-one:

#### Line 84
`items = items.slice(0, index - 1)` -> I don't see any way this can add an `undefined` value to the array ❌ 

#### Line 86
`items.push({ path, state, id })` -> At worst, this could append an object like: `{path: undefined, state: undefined, id: undefined}`, but we wouldn't end up with `undefined` itself in the `items` array. So not here either ❌ 

#### Line 108
`items = [{ path, state, id }];` -> Similar to line 86, this can't cause `undefined` to be in the `items` array ❌ 

#### Line 110
`items[index] = { path, state, id };` -> This _can_ cause `undefined` to be in the `items` array if `index` is greater than the length of the array, like so:

```js
> let items = [];
undefined
> items[1] = 'Hello';
'Hello'
> items[0];
undefined
```

It seems this is our problem line, and tells us that the `index` is becoming greater than the length of the items array. I believe I found the only location in this component where that could happen, and fix it in this PR.

Let me know if you have questions or concerns! If anyone from the community wants to help out with providing testing or consistent reproduction steps, that would be great!
@chrda81

chrda81 commented Feb 7, 2022

Copy link
Copy Markdown

There is another issue with the new code lines regarding item.id. This only seems to happen sometimes, when I click on the first drawer item. Please see the screenshot attached. And sometimes the error isn't thrown, but the browser seems to go backwards after clicking the first drawer item, which means leaving the website.
image

@listiani13

Copy link
Copy Markdown
Contributor

Hey @chrda81! I'm also running into this error intermittently & planning to debug, but unable to reproduce. Could you tell the step by step to repro for the drawer (unsure, is this also intermittent for drawer)? A video would be great! Thank you 😄

Another note: this seems to happen a lot in Chrome (unsure if this is related)

@ruida-shen

Copy link
Copy Markdown

There is another issue with the new code lines regarding item.id. This only seems to happen sometimes, when I click on the first drawer item. Please see the screenshot attached. And sometimes the error isn't thrown, but the browser seems to go backwards after clicking the first drawer item, which means leaving the website. image

I am running into the same bug as well, and weirdly it only happens to the first drawer/bottom tab item.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants