diff --git a/src/builtins/set.rs b/src/builtins/set.rs index 54574e19c..c96f6f32b 100644 --- a/src/builtins/set.rs +++ b/src/builtins/set.rs @@ -937,6 +937,17 @@ fn set_internal( // Setting with explicit indexes like `set foo[3] ...` has additional error handling. if !split.indexes.is_empty() { + // Indexes must be > 0. (Note split_var_and_indexes negates negative values). + for ind in &split.indexes { + if *ind <= 0 { + streams.err.append(wgettext_fmt!( + "%ls: array index out of bounds\n", + cmd + )); + builtin_print_error_trailer(parser, streams.err, cmd); + return STATUS_INVALID_ARGS; + } + } // Append and prepend are disallowed. if opts.append || opts.prepend { streams.err.append(wgettext_fmt!( diff --git a/tests/checks/set.fish b/tests/checks/set.fish index 20e2f68a8..68d4ab9e9 100644 --- a/tests/checks/set.fish +++ b/tests/checks/set.fish @@ -1001,4 +1001,16 @@ env -u XDG_CONFIG_HOME HOME=$PWD/empty LC_ALL=en_US.UTF-8 $FISH -c 'set -S LC_AL # CHECK: $LC_ALL[1]: |en_US.UTF-8| # CHECK: $LC_ALL: originally inherited as |en_US.UTF-8| +# This used to crash +set line[0] "" +# CHECKERR: set: array index out of bounds +# CHECKERR: {{.*}}set.fish (line {{\d+}}): +# CHECKERR: set line[0] "" +# CHECKERR: ^ +# CHECKERR: (Type 'help set' for related documentation) + + +echo Still here +# CHECK: Still here + exit 0