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

Use wrapping sub to prevent underflow #4

Merged
merged 2 commits into from
May 17, 2019
Merged

Use wrapping sub to prevent underflow #4

merged 2 commits into from
May 17, 2019

Conversation

casey
Copy link
Contributor

@casey casey commented Apr 24, 2019

Fix possible underflow when first diff is an addition, see #3.

@casey
Copy link
Contributor Author

casey commented Apr 24, 2019

Is wrapping sub the right behavior here? Would it be preferable to skip this test and always do format_add if this is the first diff?

@CAD97
Copy link
Owner

CAD97 commented Apr 24, 2019

This patch is easier, as a wrapping_sub will "always" be out of bounds for the get. We could treat the first diff differently, or check for the i=1 case explicitly, but I think treating every case the same is better.

@casey
Copy link
Contributor Author

casey commented Apr 24, 2019

I think that's totally reasonable. An alternative is to do:

if let Some(Rem(removed)) = i.checked_sub(1).map(|i| &diffs[i]) {

Which is weird, but maybe slightly more explicit.

@casey
Copy link
Contributor Author

casey commented May 15, 2019

What do you think about my proposal to use checked_sub? I think it's fine as written too, so please feel free to merge that if you prefer it.

@CAD97
Copy link
Owner

CAD97 commented May 15, 2019

Actually, yes, checked_sub is the correct way to do it. If you update the patch to use that I can probably push a patch update today or tomorrow.

@casey
Copy link
Contributor Author

casey commented May 15, 2019

Great, just pushed a diff w/checked_sub instead of wrapping_sub

@CAD97 CAD97 merged commit afe4234 into CAD97:master May 17, 2019
@CAD97 CAD97 mentioned this pull request May 17, 2019
@casey casey deleted the undeflow branch May 18, 2019 01:39
@casey
Copy link
Contributor Author

casey commented May 18, 2019

Sweet!

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.

None yet

2 participants