Skip to content

Commit 176401e

Browse files
committed
fix(cli): reject out-of-range durations instead of overflowing
parse_duration_to_ms multiplied the parsed number by its unit multiplier without a range check. A large --since value on `openshell logs` overflows i64: a debug build panics with "attempt to multiply with overflow", and a release build wraps to a nonsense log cutoff. Use checked_mul and return a diagnostic naming the offending input. Every in-range result is unchanged, and the largest accepted duration still leaves the caller's `now_ms - dur_ms` well inside i64. Signed-off-by: Nathan DeMoss <ndemoss28@gmail.com>
1 parent c27a3a3 commit 176401e

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

crates/openshell-cli/src/commands/common.rs

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -743,7 +743,9 @@ pub fn parse_duration_to_ms(s: &str) -> Result<i64> {
743743
));
744744
}
745745
};
746-
Ok(num * multiplier)
746+
num.checked_mul(multiplier).ok_or_else(|| {
747+
miette::miette!("duration out of range: {s} (must fit in milliseconds as a 64-bit integer)")
748+
})
747749
}
748750

749751
// ---------------------------------------------------------------------------
@@ -1073,6 +1075,24 @@ mod tests {
10731075
assert!(err.to_string().contains("invalid duration"));
10741076
}
10751077

1078+
#[test]
1079+
fn parse_duration_to_ms_rejects_out_of_range_values_without_overflowing() {
1080+
let err = parse_duration_to_ms("9223372036854775807h").expect_err("overflow should error");
1081+
assert!(err.to_string().contains("duration out of range"));
1082+
1083+
let err = parse_duration_to_ms("-9223372036854775808h").expect_err("overflow should error");
1084+
assert!(err.to_string().contains("duration out of range"));
1085+
}
1086+
1087+
#[test]
1088+
fn parse_duration_to_ms_accepts_the_largest_representable_duration() {
1089+
let max_hours = i64::MAX / 3_600_000;
1090+
assert_eq!(
1091+
parse_duration_to_ms(&format!("{max_hours}h")).expect("parse"),
1092+
max_hours * 3_600_000
1093+
);
1094+
}
1095+
10761096
#[test]
10771097
fn platform_progress_events_update_borrowed_display_without_duplicate_steps() {
10781098
let event = PlatformEvent {

0 commit comments

Comments
 (0)