From 75fedcc8dd082c22ca9077449cf4313342712b8f Mon Sep 17 00:00:00 2001 From: pwygab <88221256+merelymyself@users.noreply.github.com> Date: Sat, 6 Apr 2024 22:03:05 +0800 Subject: [PATCH] prevent `select` (negative number) from hanging shell (#12393) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit # Description Resolves #11756. Resolves #12346. As per description, shell no longer hangs: ``` ~/CodingProjects/nushell> [1 2 3] | select (-2) Error: nu::shell::cant_convert × Can't convert to cell path. ╭─[entry #1:1:18] 1 │ [1 2 3] | select (-2) · ──┬─ · ╰── can't convert negative number to cell path ╰──── ``` # User-Facing Changes # Tests + Formatting Added relevant test :rocket: # After Submitting Possibly support `get` `get`ting negative numbers, as per #12346 discussion. Alternatively, we can consider adding a cellpath for negative indexing? --- crates/nu-command/src/filters/select.rs | 10 +++++++++- crates/nu-command/tests/commands/select.rs | 6 ++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/crates/nu-command/src/filters/select.rs b/crates/nu-command/src/filters/select.rs index dc9da1054c..916a4e8e5e 100644 --- a/crates/nu-command/src/filters/select.rs +++ b/crates/nu-command/src/filters/select.rs @@ -71,7 +71,15 @@ produce a table, a list will produce a list, and a record will produce a record. }; new_columns.push(cv.clone()); } - Value::Int { val, .. } => { + Value::Int { val, internal_span } => { + if val < 0 { + return Err(ShellError::CantConvert { + to_type: "cell path".into(), + from_type: "negative number".into(), + span: internal_span, + help: None, + }); + } let cv = CellPath { members: vec![PathMember::Int { val: val as usize, diff --git a/crates/nu-command/tests/commands/select.rs b/crates/nu-command/tests/commands/select.rs index 7f2e9b8b57..028ecc3db7 100644 --- a/crates/nu-command/tests/commands/select.rs +++ b/crates/nu-command/tests/commands/select.rs @@ -275,3 +275,9 @@ fn select_single_row_with_variable() { assert_eq!(actual.out, "[[a]; [3]]".to_string()); assert!(actual.err.is_empty()); } + +#[test] +fn select_with_negative_number_errors_out() { + let actual = nu!("[1 2 3] | select (-2)"); + assert!(actual.err.contains("negative number")); +}