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 panic trim_first_and_last_line_of_whitespace on bad slice indices #865

Merged

Conversation

CosmicHorrorDev
Copy link
Contributor

Fixes: #727

I believe this appropriately fixes the panic found in #727 where the end index would end up preceding the start.

If this doesn't match the expected behavior of the method then let me know. I didn't see any comments or example tests, so I was mostly just guessing at how it should behave for that case. It seemed like the condition checking for self[i.saturating_sub(1)] == b'\r' was to handle stripping off the full b"\r\n", so I updated the condition to handle that case specifically

The end index could also be out of the range of the slice too since it's fallback value is self.len() while the slicing uses an inclusive range

I ended up adding a basic property-based test along with regression tests to be more confident in the implementation which is what ended up catching the case with end index's fallback value

@XAMPPRocky
Copy link
Owner

Thank you for your PR! I’ve been quite busy with other projects so I definitely appreciate someone coming along and fixing these issues. 🙂

@XAMPPRocky XAMPPRocky merged commit 0152709 into XAMPPRocky:master Dec 20, 2021
@CosmicHorrorDev
Copy link
Contributor Author

tokei is one of the very first rust projects I learned of way back when I first started learning Rust, so I'm just happy to be able to contribute back :)

CIAvash pushed a commit to CIAvash/tokei that referenced this pull request Aug 14, 2022
…es (XAMPPRocky#865)

* Fix panic from slice end being before slice start

* Fix fallback value for end index

Co-authored-by: XAMPPRocky <4464295+XAMPPRocky@users.noreply.github.com>
kornysietsma pushed a commit to kornysietsma/tokei that referenced this pull request Nov 16, 2022
…es (XAMPPRocky#865)

* Fix panic from slice end being before slice start

* Fix fallback value for end index

Co-authored-by: XAMPPRocky <4464295+XAMPPRocky@users.noreply.github.com>
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.

panic in trim_first_and_last_line_of_whitespace
2 participants