Skip to content

fix(builtins): abort declare/local on out-of-range negative subscripts (#635) - #658

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/issue635-builtin-oob
Oct 16, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/issue635-builtin-oob

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

fix(builtins): abort declare/local on out-of-range negative subscripts (#635)

What

declare -a a=( [-2]=q ) printed AFTER with status 0 and an empty
array. GNU computes max+1+ind and, still negative, reports
[-2]=q: bad array subscript on the whole word with no AFTER
(arrayfunc.c:804-809 + assign_array_var_from_string NULL +
bash_variable_assignment_error DISCARD). The assignment route was fixed
by #631/#634; the builtin route conflated "no [..] header" with "out
of range" (both None) and skipped the element.

Fix

  • array_assignment_index returns a 3-state enum (Index / NoHeader /
    NegativeOutOfRange) instead of Option; [] keeps its historical
    append-next meaning (GNU's err_badarraysub for it is a separate,
    deliberately untouched concern).
  • append_array_value breaks the scan on OOB with prior elements kept
    and reports the whole word up through an out-param; every call layer
    (assign_declare_names, the declare executor fn, export/readonly
    value computation) threads it without changing failglob handling.
  • declare/local translate at the Executor layer (mirroring the
    arith_eval_failure precedent, but with the bare report_error
    diagnostic — no command: segment): partial store already installed,
    exit 1, DISCARD abort. export/readonly record failure status with
    the same diagnostic; their list-abort threading is a documented
    follow-up (no battery coverage there yet).
  • Eval containment reuses the eval does not contain the assignment DISCARD jump (eval 'a[-2]=q' ends the whole script) #637 absorption primitive (armed alongside
    the abort), so eval returns failure with the partial store and the
    caller continues.

Verification

  • New 20-case GNU differential battery: 6/20 on master → 20/20
    (declare/append/local/typeset/eval/subshell/trap/controls).
  • New tests/issue635_builtin_oob.rs (10 tests).
  • Focused suites green (parity, 631, 637, 636, 638, 639,
    compound-quote and neighbors); full serial failure-set gate vs d4d8dc4:
    target sets identical (NEW=0/DISAPPEARED=0; branch +1 ok = the new test
    file); test-level 2 NEW / 2 DISAPPEARED all in the
    known-nondeterministic part_080 family (all four pass in isolation on
    branch).
  • niubash product A/B (niubash pinned 6295367, only rubash [patch]
    switched): failure sets byte-identical (49=49).

GNU owners: arrayfunc.c:753-809 (header scan), :913-932
(assign_array_var_from_string), subst.c:11169
(bash_variable_assignment_error), arrayfunc.c:63 (badsub message).

#635)

declare -a a=( [-2]=q ) printed AFTER with status 0 and an empty array.
GNU computes max+1+ind and, still negative, reports [-2]=q: bad array
subscript on the whole word with no AFTER (arrayfunc.c:804-809 +
assign_array_var_from_string NULL + bash_variable_assignment_error
DISCARD). The assignment route was fixed by #631/#634; the builtin route
conflated "no [..] header" with "out of range" and skipped the element.

array_assignment_index now resolves 3 states (Index / NoHeader /
NegativeOutOfRange); the scan breaks with prior elements kept and the
whole word travels up via out-params (append fn -> assign fns ->
declare executor fn). declare/local translate at the Executor layer
mirroring the arith_eval_failure precedent but with the bare report_error
diagnostic (no command: segment): partial already installed, exit 1,
DISCARD abort. Eval containment reuses the #637 absorption primitive.
export/readonly record failure status with the same diagnostic; their
list-abort threading is a documented follow-up.

Verification: new 20-case GNU differential 6/20 -> 20/20; new
tests/issue635_builtin_oob.rs (10 tests); focused suites green; full
serial failure-set gate vs d4d8dc4 identical targets (NEW=0/
DISAPPEARED=0; 2/2 part_080 coproc flake passing in isolation); niubash
A/B failure sets byte-identical (49=49).
@caomengxuan666
caomengxuan666 merged commit 287fbe7 into master Oct 16, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/issue635-builtin-oob branch October 16, 2026 08:46
caomengxuan666 added a commit that referenced this pull request Oct 16, 2026
caomengxuan666 added a commit that referenced this pull request Oct 16, 2026
… (#677)

export -a a=( [-2]=q ); echo AFTER printed AFTER rc 0; GNU diagnoses,
no AFTER, rc 1 (same readonly -a). export -a/-A delegates to nested
declare_builtin (setattr.def:240-268): arrayfunc.c:804-809 any_failed +
DISCARD (subst.c:11169).

Mirrors declare/local #658: apply arms record oob_word, keep partial
store, stop the operand loop; Executor arms print the bare diagnostic,
exit 1, raise the #637 evalerror abort. 13 new tests; 18/18 GNU probe
matrix.

Co-authored-by: caomengxuan666 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant