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

Fallback to original URL if the beginIndex is invalid #143

Merged
merged 3 commits into from
Mar 14, 2024

Conversation

antonis
Copy link

@antonis antonis commented Mar 13, 2024

Fixes wordpress-mobile/WordPress-Android#18626

WordPress-Android PR: wordpress-mobile/WordPress-Android#20459

Description

Checks the begin index before calling the substring function to prevent the StringIndexOutOfBoundsException

To test

I was not able to reproduce the issue but there are 5 different crashes rooting from this exception (see wordpress-mobile/WordPress-Android#18626 (comment))
I managed to fake one URL that reproduces the crash and added a test for it.

@antonis antonis added the bug label Mar 13, 2024
return "https://i0.wp.com/" + imageUrl.substring(schemePos + 3, imageUrl.length()) + query;
int beginIndex = schemePos + 3;
if (beginIndex < 0 || beginIndex > imageUrl.length()) {
// Fallback to original URL if the beginIndex is invalid to avoid `StringIndexOutOfBoundsException`
Copy link
Contributor

Choose a reason for hiding this comment

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

NP: It seems that imageUrl is conditionally mutated a few times prior to this. Should we tweak this code comment to not mention "original" URL? Or, alternatively, if that is what we want, store it in a local var at the top of the function and return it here without mutation? 🤔

Copy link
Author

Choose a reason for hiding this comment

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

That's a good suggestion @mkevins 👍
In the other scheme error that the imageUrl is returned it hasn't been mutated yet. I think it makes sense to keep a copy of the url before any changes and return that.
I changed the code with e6b28eb

Copy link
Contributor

@mkevins mkevins left a comment

Choose a reason for hiding this comment

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

I left a couple of minor questions, but I consider them non-blocking, since I believe the implementation changes here are already an improvement over crashing out of bounds.

@antonis
Copy link
Author

antonis commented Mar 14, 2024

Thank you for reviewing and your feedback @mkevins 🙇

@antonis antonis merged commit 4d6cb9a into trunk Mar 14, 2024
8 checks passed
@antonis antonis deleted the fix/getPhotonImageUrlIndexOutOfBounds branch March 14, 2024 06:58
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

StringIndexOutOfBoundsException: PhotonUtils.getPhotonImageUrl
2 participants