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

Fix panic when parsing connection timeout #295

Merged
merged 2 commits into from
Dec 14, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 14 additions & 7 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1070,13 +1070,20 @@ impl FromStr for Timeout {
type Err = anyhow::Error;

fn from_str(sec: &str) -> anyhow::Result<Timeout> {
let pos_sec: f64 = match sec.parse::<f64>() {
Ok(sec) if sec.is_sign_positive() => sec,
_ => return Err(anyhow!("Invalid seconds as connection timeout")),
};

let dur = Duration::from_secs_f64(pos_sec);
Ok(Timeout(dur))
match f64::from_str(sec) {
Ok(s) => {
if !s.is_finite() {
Err(anyhow!("Connection timeout is not finite"))
} else if s.is_sign_negative() {
Err(anyhow!("Connection timeout is negative"))
} else if s >= Duration::MAX.as_secs_f64() {
Err(anyhow!("Connection timeout is too big"))
} else {
Ok(Timeout(Duration::from_secs_f64(s)))
}
}
_ => Err(anyhow!("Connection timeout is not float value")),
}
sorairolake marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand Down
20 changes: 19 additions & 1 deletion tests/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -661,11 +661,29 @@ fn timeout_no_limit() {

#[test]
fn timeout_invalid() {
get_command()
.args(["--timeout=inf", "--offline", ":"])
.assert()
.failure()
.stderr(contains("Connection timeout is not finite"));

get_command()
.args(["--timeout=-0.01", "--offline", ":"])
.assert()
.failure()
.stderr(contains("Invalid seconds as connection timeout"));
.stderr(contains("Connection timeout is negative"));

get_command()
.args(["--timeout=18446744073709552000", "--offline", ":"])
.assert()
.failure()
.stderr(contains("Connection timeout is too big"));

get_command()
.args(["--timeout=SEC", "--offline", ":"])
.assert()
.failure()
.stderr(contains("Connection timeout is not float value"));
}

#[test]
Expand Down