-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Panics when parsing a single hyphen #410
Comments
Is there a reason that fn starts_with(&self, s: &[u8]) -> bool {
self.as_bytes().starts_with(s)
} |
Thanks for taking the time to file this! I see you pushed a commit to your fork, feel free to submit the PR! |
@cite-reader Also, your commit message is excellent! This is one of many things I need to work harder on (read, "actually do"). |
fix: Stop lonely hyphens from causing panic The method `starts_with` as implemented for the `OsStrExt2` trait on `OsStr` assumed that the needle given is shorter than the haystack. When this is not the case, the method panics due to an attempted out-of-bounds access on the byte representation of `self`. Problematic if, say, an end-user gives us `"-"` and the library tries to see if that starts with `"--"`. Fortunately, slices already implement a `starts_with` method, and we can delegate to it. This *does* create a semantics change: if both `self` and the needle have length 0, this implementation will return `true`, but the old implementation would return `false`. Based on the test suite still passing, acknowledging the vacuous truth doesn't seem to cause any problems. Fixes #410
fix: Stop lonely hyphens from causing panic The method `starts_with` as implemented for the `OsStrExt2` trait on `OsStr` assumed that the needle given is shorter than the haystack. When this is not the case, the method panics due to an attempted out-of-bounds access on the byte representation of `self`. Problematic if, say, an end-user gives us `"-"` and the library tries to see if that starts with `"--"`. Fortunately, slices already implement a `starts_with` method, and we can delegate to it. This *does* create a semantics change: if both `self` and the needle have length 0, this implementation will return `true`, but the old implementation would return `false`. Based on the test suite still passing, acknowledging the vacuous truth doesn't seem to cause any problems. Fixes #410
once #413 merges I'll put out the new version on crates.io That PR also adds tests so we shouldn't run into this issue again. |
Given the following minimal test case:
running the resulting executable as
./target/debug/repro -
produces the errorthread '<main>' panicked at 'index out of bounds: the len is 1 but the index is 1', /home/cite-reader/.multirust/toolchains/stable/cargo/registry/src/jackfan.us.kg-88ac128001ac3a9a/clap-2.0.2/src/osstringext.rs:42
.Running with
RUST_BACKTRACE=1
additionally produces this wall of text:The text was updated successfully, but these errors were encountered: