From c857e18c4a83f2df36733e955301f89ee321f428 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9s=20N=2E=20Robalino?= Date: Tue, 24 Nov 2020 05:50:38 -0500 Subject: [PATCH] Avoid subtract overflow when no ending index given. (#2764) --- crates/nu-cli/src/commands/str_/substring.rs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/nu-cli/src/commands/str_/substring.rs b/crates/nu-cli/src/commands/str_/substring.rs index cbfa4a4b3..dea5e13e0 100644 --- a/crates/nu-cli/src/commands/str_/substring.rs +++ b/crates/nu-cli/src/commands/str_/substring.rs @@ -159,12 +159,14 @@ fn action(input: &Value, options: &Substring, tag: impl Into) -> Result Ok(UntaggedValue::string( + Ordering::Less => Ok(UntaggedValue::string(if end == isize::max_value() { + s.chars().skip(start as usize).collect::() + } else { s.chars() .skip(start as usize) .take((end - start) as usize) - .collect::(), - ) + .collect::() + }) .into_value(tag)), } } else { @@ -330,6 +332,9 @@ mod tests { expectation("and", (0, -3)), expectation("andr", (0, -2)), expectation("andre", (0, -1)), + // str substring [ -4 , _ ] + // str substring -4 , + expectation("dres", (-4, isize::max_value())), expectation("", (0, -110)), expectation("", (6, 0)), expectation("", (6, -1)),