From 6c17c42d3b66c3503e3ebd06fb051725e40ff851 Mon Sep 17 00:00:00 2001 From: reuben olinsky Date: Thu, 24 Oct 2024 21:56:03 -0700 Subject: [PATCH] fix(completion): correct behavior of slice past end of array --- brush-core/src/expansion.rs | 4 +--- brush-shell/tests/cases/word_expansion.yaml | 10 ++++++++++ 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/brush-core/src/expansion.rs b/brush-core/src/expansion.rs index d78a4db6..7263b594 100644 --- a/brush-core/src/expansion.rs +++ b/brush-core/src/expansion.rs @@ -853,9 +853,7 @@ impl<'a> WordExpander<'a> { let expanded_offset = usize::try_from(expanded_offset)?; let expanded_parameter_len = expanded_parameter.polymorphic_len(); - if expanded_offset >= expanded_parameter_len { - return Ok(Expansion::from(String::new())); - } + let expanded_offset = min(expanded_offset, expanded_parameter_len); let end_offset = if let Some(length) = length { let mut expanded_length = length.eval(self.shell, false).await?; diff --git a/brush-shell/tests/cases/word_expansion.yaml b/brush-shell/tests/cases/word_expansion.yaml index 5350e650..93a239d3 100644 --- a/brush-shell/tests/cases/word_expansion.yaml +++ b/brush-shell/tests/cases/word_expansion.yaml @@ -668,6 +668,16 @@ cases: echo "\${myarray[@]:2:2}: ${myarray[@]:2:2}" echo "\${myarray[@]:2}: ${myarray[@]:2}" + - name: "Substring operator past end of array" + stdin: | + set a b c + declare -a result1=("${@:3}") + declare -p result1 + + myarray=(a b c) + declare -a result2=("${myarray[@]:3}") + declare -p result2 + - name: "Substring with length (with nested expressions)" stdin: | var="Hello, world!"