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

timeout: avoid panicking for empty string #3206

Merged
merged 1 commit into from
Mar 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
15 changes: 9 additions & 6 deletions src/uu/timeout/src/timeout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ struct Config {
}

impl Config {
fn from(options: &clap::ArgMatches) -> Self {
fn from(options: &clap::ArgMatches) -> UResult<Self> {
let signal = match options.value_of(options::SIGNAL) {
Some(signal_) => {
let signal_result = signal_by_name_or_value(signal_);
Expand All @@ -69,8 +69,11 @@ impl Config {
.value_of(options::KILL_AFTER)
.map(|time| uucore::parse_time::from_str(time).unwrap());

let duration: Duration =
uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()).unwrap();
let duration =
match uucore::parse_time::from_str(options.value_of(options::DURATION).unwrap()) {
Ok(duration) => duration,
Err(err) => return Err(USimpleError::new(1, err)),
};

let preserve_status: bool = options.is_present(options::PRESERVE_STATUS);
let foreground = options.is_present(options::FOREGROUND);
Expand All @@ -82,15 +85,15 @@ impl Config {
.map(String::from)
.collect::<Vec<_>>();

Self {
Ok(Self {
foreground,
kill_after,
signal,
duration,
preserve_status,
verbose,
command,
}
})
}
}

Expand All @@ -104,7 +107,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

let matches = app.get_matches_from(args);

let config = Config::from(&matches);
let config = Config::from(&matches)?;
timeout(
&config.command,
config.duration,
Expand Down