From 2f65b29866d78da1fb3b6559ad6c2ad810c74341 Mon Sep 17 00:00:00 2001 From: Jeffrey Finkelstein Date: Thu, 10 Feb 2022 19:16:49 -0500 Subject: [PATCH] split: error when --additional-suffix contains / Make `split` terminate with a usage error when the `--additional-suffix` argument contains a directory separator character. --- src/uu/split/src/split.rs | 19 +++++++++++++++++-- tests/by-util/test_split.rs | 8 ++++++++ 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/uu/split/src/split.rs b/src/uu/split/src/split.rs index 1b6680142..56b70bc00 100644 --- a/src/uu/split/src/split.rs +++ b/src/uu/split/src/split.rs @@ -264,6 +264,9 @@ enum SettingsError { /// Invalid suffix length parameter. SuffixLength(String), + /// Suffix contains a directory separator, which is not allowed. + SuffixContainsSeparator(String), + /// The `--filter` option is not supported on Windows. #[cfg(windows)] NotSupported, @@ -272,7 +275,10 @@ enum SettingsError { impl SettingsError { /// Whether the error demands a usage message. fn requires_usage(&self) -> bool { - matches!(self, Self::Strategy(StrategyError::MultipleWays)) + matches!( + self, + Self::Strategy(StrategyError::MultipleWays) | Self::SuffixContainsSeparator(_) + ) } } @@ -281,6 +287,11 @@ impl fmt::Display for SettingsError { match self { Self::Strategy(e) => e.fmt(f), Self::SuffixLength(s) => write!(f, "invalid suffix length: {}", s.quote()), + Self::SuffixContainsSeparator(s) => write!( + f, + "invalid suffix {}, contains directory separator", + s.quote() + ), #[cfg(windows)] Self::NotSupported => write!( f, @@ -294,13 +305,17 @@ impl fmt::Display for SettingsError { impl Settings { /// Parse a strategy from the command-line arguments. fn from(matches: &ArgMatches) -> Result { + let additional_suffix = matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_string(); + if additional_suffix.contains('/') { + return Err(SettingsError::SuffixContainsSeparator(additional_suffix)); + } let suffix_length_str = matches.value_of(OPT_SUFFIX_LENGTH).unwrap(); let result = Self { suffix_length: suffix_length_str .parse() .map_err(|_| SettingsError::SuffixLength(suffix_length_str.to_string()))?, numeric_suffix: matches.occurrences_of(OPT_NUMERIC_SUFFIXES) > 0, - additional_suffix: matches.value_of(OPT_ADDITIONAL_SUFFIX).unwrap().to_owned(), + additional_suffix, verbose: matches.occurrences_of("verbose") > 0, strategy: Strategy::from(matches).map_err(SettingsError::Strategy)?, input: matches.value_of(ARG_INPUT).unwrap().to_owned(), diff --git a/tests/by-util/test_split.rs b/tests/by-util/test_split.rs index 911a7bf30..59b84fdf8 100644 --- a/tests/by-util/test_split.rs +++ b/tests/by-util/test_split.rs @@ -228,6 +228,14 @@ fn test_split_additional_suffix() { assert_eq!(glob.collate(), at.read_bytes(name)); } +#[test] +fn test_additional_suffix_no_slash() { + new_ucmd!() + .args(&["--additional-suffix", "a/b"]) + .fails() + .usage_error("invalid suffix 'a/b', contains directory separator"); +} + // note: the test_filter* tests below are unix-only // windows support has been waived for now because of the difficulty of getting // the `cmd` call right