Skip to content

Commit 533a4e7

Browse files
When parsing extremely large durations (like i64::MAX days), the conversion to seconds overflows. Instead of failing, cap the duration at time_t::MAX seconds, which is the maximum valid timeout value.
1 parent 72d5443 commit 533a4e7

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

src/uu/timeout/src/timeout.rs

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,23 @@ impl Config {
8383
},
8484
};
8585

86-
let duration =
87-
parse_time::from_str(options.get_one::<String>(options::DURATION).unwrap(), true)
88-
.map_err(|err| UUsageError::new(ExitStatus::TimeoutFailed.into(), err))?;
86+
let duration = match parse_time::from_str(
87+
options.get_one::<String>(options::DURATION).unwrap(),
88+
true,
89+
) {
90+
Ok(d) => d,
91+
Err(err) => {
92+
// If parsing fails due to overflow, use maximum valid duration
93+
// This handles cases like "9223372036854775808d" (i64::MAX days)
94+
let err_str = err.to_string();
95+
if err_str.contains("overflow") || err_str.contains("too large") {
96+
// Cap at time_t::MAX seconds (maximum valid timeout)
97+
Duration::from_secs(libc::time_t::MAX as u64)
98+
} else {
99+
return Err(UUsageError::new(ExitStatus::TimeoutFailed.into(), err));
100+
}
101+
}
102+
};
89103

90104
let preserve_status: bool = options.get_flag(options::PRESERVE_STATUS);
91105
let foreground = options.get_flag(options::FOREGROUND);

0 commit comments

Comments
 (0)