Skip to content

declare -a a=( [-2]=q ) / local -a a=( [-2]=q ) still drop an out-of-range negative subscript (builtin route of #631) #635

Closed

Description

@caomengxuan666

Summary

The same out-of-range negative subscript that #631 fixes on the assignment
route is still dropped in silence on the builtin route:

$ bash -c 'declare -a a=( [-2]=q ); echo AFTER; declare -p a'
bash: line 1: [-2]=q: bad array subscript
$ echo $?
1                      # no AFTER — the run ends

$ ./rubash -c 'declare -a a=( [-2]=q ); echo AFTER; declare -p a'
AFTER
declare -a a=()
$ echo $?
0                      # element dropped in silence

Same for local and typeset inside a function:

$ bash -c 'f(){ local -a a=( [-2]=q ); echo IN; }; f; echo AFTER'
bash: line 1: [-2]=q: bad array subscript
$ ./rubash -c 'f(){ local -a a=( [-2]=q ); echo IN; }; f; echo AFTER'
IN
AFTER

Also declare -a a; a+=( [-2]=q ).

GNU (the spec)

assign_compound_array_list (third_party/bash/arrayfunc.c:700) computes the
element subscript, counts a negative one back from the end
(array_max_index + 1 + ind, arrayfunc.c:802-803) and, when it is still
below zero, reports err_badarraysub (w) — the whole element word, through
report_error (error.c:440; message arrayfunc.c:63) — counts any_failed
and breaks the list (arrayfunc.c:804-809). assign_array_var_from_string
(arrayfunc.c:913-932) then returns NULL, so the assignment fails:
bash_variable_assignment_error (subst.c:11169) → exp_jump_to_top_level (DISCARD) (subst.c:11174). The elements inserted before the failing one stay
stored.

Both the assignment route and the builtin route reach the GNU builtin through
the same function, so both must behave identically.

Root cause

src/builtins/declare/storage/array.rs:239 array_assignment_index is a second
implementation of the same resolver, and it has the identical defect fixed for
the executor copy in #631: it returns Option<usize>, so "not a [subscript]
header" and "this subscript resolved out of range" are conflated. The call
sites (:106, :129, :135, :147) then continue via
array_assignment_has_subscript(left) and the element vanishes.

Why it is not fixed together with #631

The declare-side scan returns Result<String, String> into the declare builtin,
which holds a plain variables map and has no Executor handle, so it
cannot arm the fatal DISCARD jump that the assignment route uses
(raise_evalerror_abort). Fixing it means giving the builtin its own fatal
error channel (a new error variant threaded through the declare/local/
typeset call sites), which is a different design change from #631's
ArrayAssignmentIndex fix.

Refs #631 (assignment route, fixed by #634).

Activity

  1. added
    bug
    P2-medium
    on Oct 15, 2026
  2. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Fix is up as PR #658 (branch fix/issue635-builtin-oob on d4d8dc4). 3-state resolver + break-with-partial + out-param word chain + Executor-layer abort mirroring the arith_eval_failure precedent; eval containment reuses #637's primitive. Battery 6/20 -> 20/20. Regression file tests/issue635_builtin_oob.rs (10 tests). export/readonly list-abort threading left as documented follow-up.

  3. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Landed as #658 (squash-merged 287fbe7). Battery 6/20 -> 20/20. export/readonly list-abort threading left as documented follow-up.

  4. caomengxuan666 commented on Oct 16, 2026

    @caomengxuan666
    MemberAuthor

    Export/readonly follow-up cut as PR #677 (branch fix/issue635-export-abort): mirrors #658 (oob_word threading, operand-loop stop, bare diagnostic, #637 abort). 13 new tests; 18/18 GNU probe matrix. Gate running.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

P2-mediumbug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions