diff --git a/crates/nu-command/src/bytes/at.rs b/crates/nu-command/src/bytes/at.rs index f6d98ba119..9663e545a4 100644 --- a/crates/nu-command/src/bytes/at.rs +++ b/crates/nu-command/src/bytes/at.rs @@ -140,7 +140,25 @@ fn map_value(input: &Value, args: &Arguments, head: Span) -> Value { match input { Value::Binary { val, .. } => { let len = val.len() as u64; - let start: usize = range.absolute_start(len).try_into().unwrap(); + let start: u64 = range.absolute_start(len); + let start: usize = match start.try_into() { + Ok(start) => start, + Err(_) => { + let span = input.span(); + return Value::error( + ShellError::UnsupportedInput { + msg: format!( + "Absolute start position {start} was too large for your system arch." + ), + input: args.range.to_string(), + msg_span: span, + input_span: span, + }, + head, + ); + } + }; + let bytes: Vec = match range.absolute_end(len) { Bound::Unbounded => val[start..].into(), Bound::Included(end) => val[start..=end as usize].into(), diff --git a/crates/nu-protocol/src/value/range.rs b/crates/nu-protocol/src/value/range.rs index 9e9c252fdd..a01cebe78f 100644 --- a/crates/nu-protocol/src/value/range.rs +++ b/crates/nu-protocol/src/value/range.rs @@ -81,10 +81,11 @@ mod int_range { self.start } + // Resolves the absolute start position given the length of the input value pub fn absolute_start(&self, len: u64) -> u64 { let max_index = len - 1; match self.start { - start if start < 0 => len.saturating_sub(start.unsigned_abs().into()), + start if start < 0 => len.saturating_sub(start.unsigned_abs()), start => max_index.min(start as u64), } } @@ -110,11 +111,11 @@ mod int_range { match self.end { Bound::Unbounded => Bound::Unbounded, Bound::Included(i) => Bound::Included(match i { - i if i < 0 => len.saturating_sub(i.unsigned_abs() as u64), + i if i < 0 => len.saturating_sub(i.unsigned_abs()), i => max_index.min(i as u64), }), Bound::Excluded(i) => Bound::Excluded(match i { - i if i < 0 => len.saturating_sub(i.unsigned_abs() as u64), + i if i < 0 => len.saturating_sub(i.unsigned_abs()), i => len.min(i as u64), }), }