-
Notifications
You must be signed in to change notification settings - Fork 44
diff: prevent --tabsize from crashing on huge values #292
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,6 +4,9 @@ use std::path::PathBuf; | |
|
|
||
| use regex::Regex; | ||
|
|
||
| /// Largest value accepted for `--tabsize`, matching GNU diff. | ||
| const MAX_TABSIZE: usize = isize::MAX as usize - 3; | ||
|
|
||
| #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)] | ||
| pub enum Format { | ||
| #[default] | ||
|
|
@@ -144,14 +147,8 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu | |
| .unwrap() | ||
| .as_str(); | ||
| params.tabsize = match tabsize_str.parse::<usize>() { | ||
| Ok(num) => { | ||
| if num == 0 { | ||
| return Err("invalid tabsize «0»".to_string()); | ||
| } | ||
|
|
||
| num | ||
| } | ||
| Err(_) => return Err(format!("invalid tabsize «{tabsize_str}»")), | ||
| Ok(num) if num != 0 && num <= MAX_TABSIZE => num, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this doesn't fix the crash, it just moves it
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, it did just move the crash. I've changed |
||
| _ => return Err(format!("invalid tabsize «{tabsize_str}»")), | ||
| }; | ||
|
|
||
| continue; | ||
|
|
@@ -811,6 +808,42 @@ mod tests { | |
| .peekable() | ||
| ) | ||
| .is_err()); | ||
| for too_large in [(MAX_TABSIZE + 1).to_string(), usize::MAX.to_string()] { | ||
| assert_eq!( | ||
| Err(format!("invalid tabsize «{too_large}»")), | ||
| parse_params( | ||
| [ | ||
| os("diff"), | ||
| os(&format!("--tabsize={too_large}")), | ||
| os("foo"), | ||
| os("bar") | ||
| ] | ||
| .iter() | ||
| .cloned() | ||
| .peekable() | ||
| ) | ||
| ); | ||
| } | ||
| assert_eq!( | ||
| Ok(Params { | ||
| executable: os("diff"), | ||
| from: os("foo"), | ||
| to: os("bar"), | ||
| tabsize: MAX_TABSIZE, | ||
| ..Default::default() | ||
| }), | ||
| parse_params( | ||
| [ | ||
| os("diff"), | ||
| os(&format!("--tabsize={MAX_TABSIZE}")), | ||
| os("foo"), | ||
| os("bar") | ||
| ] | ||
| .iter() | ||
| .cloned() | ||
| .peekable() | ||
| ) | ||
| ); | ||
| } | ||
| #[test] | ||
| fn double_dash() { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
how did you get
- 3?it's not in the manual, so where does this value come from?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I found it by testing GNU diff 3.12, GNU accepts the one ending in …804 and rejects the one ending in …805. happy to change the exact limit if you want