nushell icon indicating copy to clipboard operation
nushell copied to clipboard

`into duration` overflows on large values (panic in debug, silent wrap in release)

Open santhreal opened this issue 4 months ago • 2 comments

Describe the bug

into duration multiplies the input value by its unit's nanosecond factor using unchecked arithmetic. A value large enough that value * factor exceeds i64::MAX overflows: this panics in debug builds (attempt to multiply with overflow) and silently wraps to an incorrect negative/garbage duration in release builds.

The overflow affects several code paths in into duration:

  • a unit string, e.g. 9999999999wk
  • an integer with --unit, e.g. <big> | into duration --unit wk
  • an hh:mm:ss clock string with very large hours

How to reproduce

On a debug build:

> "9999999999wk" | into duration
> 9223372036854775807 | into duration --unit wk
> "2562047788015216:00:00" | into duration

Each panics with attempt to multiply with overflow. On a release build the same inputs return a silently incorrect (wrapped) duration instead of an error.

Expected behavior

into duration should never panic on user input. A value too large to represent as a 64-bit nanosecond duration should produce a clear error rather than panicking or silently wrapping.

Configuration

Reproduces on main (0.114.2). The affected code is crates/nu-command/src/conversions/into/duration.rs (string_to_duration, compound_to_duration, parse_clock_duration, and the Value::Int arm of action).

santhreal avatar Jul 13 '26 14:07 santhreal

Adding a related detail I hit while triaging overflow behavior on main (0.116.0, 2459fdd):

The int and float paths of into duration disagree on overflow — one panics/wraps, the other silently saturates:

# int path — panics in a debug build (release wraps to a negative duration):
10000000000 | into duration --unit sec
#   × Main thread panicked.  attempt to multiply with overflow
#     at crates/nu-command/src/conversions/into/duration.rs:373:29

# float path — saturates to i64::MAX instead of erroring or wrapping:
10000000000.0 | into duration --unit sec
#   9223372036854775807ns   (= i64::MAX ns)

In duration.rs, the Value::Int arm does an unchecked *val * ns (line ~373), while the Value::Float arm goes through (*val * (ns as f64)) as i64, whose f64 -> i64 cast clamps to i64::MAX/MIN. So the same out-of-range input is a panic/word for an int but a silent clamp for a float.

Whichever resolution is chosen for #18592 (error vs. saturate), it would be good to make both the int and float arms behave the same way. The command's own help states "Max duration value is i64::MAX nanoseconds", which suggests out-of-range should be reported rather than panicking or silently clamping.

(Reported via an AI-assisted audit; I reproduced both paths against a local build.)

Master-Norna avatar Sep 29 '26 01:09 Master-Norna

resolved by #19075

fdncred avatar Oct 19 '26 11:10 fdncred