Skip to content

Commit 8fdc882

Browse files
committed
diffutils: fix panic on non-UTF-8 argument ending in --width=N
Signed-off-by: Sai Asish Y <say.apm35@gmail.com>
1 parent dc9ca17 commit 8fdc882

1 file changed

Lines changed: 35 additions & 1 deletion

File tree

src/params.rs

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub fn parse_params<I: Iterator<Item = OsString>>(mut opts: Peekable<I>) -> Resu
6060
let mut format = None;
6161
let mut context = None;
6262
let tabsize_re = Regex::new(r"^--tabsize=(?<num>\d+)$").unwrap();
63-
let width_re = Regex::new(r"--width=(?P<long>\d+)$").unwrap();
63+
let width_re = Regex::new(r"^--width=(?P<long>\d+)$").unwrap();
6464
while let Some(param) = opts.next() {
6565
let next_param = opts.peek();
6666
if param == "--" {
@@ -813,6 +813,40 @@ mod tests {
813813
.is_err());
814814
}
815815
#[test]
816+
fn width() {
817+
assert_eq!(
818+
Ok(Params {
819+
executable: os("diff"),
820+
from: os("foo"),
821+
to: os("bar"),
822+
width: 100,
823+
..Default::default()
824+
}),
825+
parse_params(
826+
[os("diff"), os("--width=100"), os("foo"), os("bar")]
827+
.iter()
828+
.cloned()
829+
.peekable()
830+
)
831+
);
832+
}
833+
#[cfg(unix)]
834+
#[test]
835+
fn width_non_utf8_is_not_an_option() {
836+
use std::os::unix::ffi::OsStringExt;
837+
// A non-UTF-8 argument whose lossy form ends in `--width=N` must be
838+
// treated as an operand, not parsed as the width option (which used to
839+
// panic in `into_string().unwrap()`).
840+
let bad = OsString::from_vec(b"\xff--width=5".to_vec());
841+
assert!(parse_params(
842+
[os("diff"), bad, os("foo"), os("bar")]
843+
.iter()
844+
.cloned()
845+
.peekable()
846+
)
847+
.is_err());
848+
}
849+
#[test]
816850
fn double_dash() {
817851
assert_eq!(
818852
Ok(Params {

0 commit comments

Comments
 (0)