Skip to content

indexOf method could return -1, Potential bug#22146

Closed
mdrazak2001 wants to merge 3 commits intofacebook:mainfrom
mdrazak2001:indexOf_-1_potentialBug
Closed

indexOf method could return -1, Potential bug#22146
mdrazak2001 wants to merge 3 commits intofacebook:mainfrom
mdrazak2001:indexOf_-1_potentialBug

Conversation

@mdrazak2001
Copy link

indexOf can cause a potential Segmentation error

the reason segmentation fault can occur is indexOf returns -1 on not finding the desired index. Hence i have added a safe if condition that is wrapped around the code which can potentially cause a segmentation error due to indexOf method.

@mdrazak2001 mdrazak2001 changed the title IndexOf method return -1, potential bug indexOf method could return -1, Potential bug Aug 20, 2021
@bvaughn bvaughn requested a review from sebmarkbage August 20, 2021 19:30
@gaearon
Copy link
Collaborator

gaearon commented Sep 6, 2021

In JavaScript you can't get a segmentation error (unless the engine is buggy). You'll get undefined which will fall through to the default case which already throws the desired error. So this is not necessary.

@gaearon gaearon closed this Sep 6, 2021
@gaearon
Copy link
Collaborator

gaearon commented Sep 6, 2021

Correction: since we're not reading from array by index but doing string manipulation, what would happen is we'd get a wrong slice of the string. Regardless, the assumption here is that input is well-formed (since we're the ones who produced it in the first place). If it's not well-formed then all bets are off, and we don't strive to produce the best possible error message anyway.

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.

3 participants