Skip to content

WIP: fix(expand): positional holders in indirect transforms (#2608) - #2609

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/2608-indirect-positional-transform
Nov 2, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/2608-indirect-positional-transform

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2608

${!N@op} — indirect expansion with a transformation operator whose target name is held in a positional parameter — silently expanded to empty: the transform entry (indirect_parameter_transform) resolved its holder in env_vars only, and positional parameters do not live there. This is the defect that made ble.sh's ble/is-array ([[ ${!1@a} == *a* ]]) report every array as a non-array (niu#805).

Fix follows GNU's own structure: parameter_brace_find_indir (subst.c:7839-7881) expands the holder as a parameter reference — "An indirect reference to a positional parameter or a special parameter is ok" (subst.c:7907-7910) — and get_var_and_type (subst.c:8540) names the transform's variable through that same resolution. The new Executor::indirect_holder_target_name factors the holder resolution the plain ${!...} path performs inline in expand_braced_special.rs (# → last positional value, ? → exit-status text, bare nameref → target name, numeric holder → positional window, name[sub] → element value, other names → scalar form via indirect_source_scalar_value), and both arms of indirect_parameter_transform now resolve through it. Unresolvable holders still fall through to the plain path / word error scan.

Tests: tests/issue2608_indirect_positional_transform_regressions.rs — the issue's three lines verbatim; @a/@Q/@A/@k/@U at $1 and @a/@A/@k/@k at $2 (assoc target → A); a name[sub] holder; an unset positional holder; [@]/[*]-arm variable-source controls. All expectations generated by running the probe scripts through GNU bash 5.3.20 as script files. Test stem UNVERIFIED-BY-CAPTAIN (cluster is code-only per the 2026-11-02 pace directive; cargo check --tests clean, per-file rustfmt clean).

… resolution

${!N@op} — indirect expansion with a transformation operator —
resolved its holder only in env_vars, so a target name held in a
positional parameter never resolved and the transform silently
expanded to empty (ble.sh's ble/is-array, [[ ${!1@a} == *a* ]], read
every array as a non-array; niu#805).

GNU subst.c parameter_brace_find_indir (7839-7881) expands the holder
as a parameter reference — subst.c:7907-7910: an indirect reference
to a positional parameter or a special parameter is ok — and
get_var_and_type (subst.c:8540) names the transform's variable
through the same resolution before parameter_brace_transform
(subst.c:8923) applies the operator to the resolved variable.

Mirror that: the new Executor::indirect_holder_target_name factors
the holder resolution the plain ${!...} path performs inline in
expand_braced_special.rs (# -> last positional value, ? -> exit
status text, bare nameref -> its target name, numeric holder ->
positional window, name[sub] -> element value, any other name -> the
variable's scalar form via indirect_source_scalar_value), and both
arms of indirect_parameter_transform resolve through it — the plain
arm for the target name, the [@]/[*] arm for a non-env source's
scalar value (env sources keep contributing raw storage,
rubash#1147). An unresolvable holder still returns None and falls
through to the plain path / word error scan, preserving the
unset-holder outcomes.

Tests: tests/issue2608_indirect_positional_transform_regressions.rs
— the issue's three lines verbatim, @a/@Q/@A/@k/@U at $1 and
@a/@A/@k/@k at $2 (assoc target reports A), a name[sub] holder, an
unset positional holder, and [@]/[*]-arm variable-source controls.
Expectations generated from GNU bash 5.3.20 script-file probes.

Refs #2608
@caomengxuan666
caomengxuan666 force-pushed the fix/2608-indirect-positional-transform branch from 2af17ba to 5f1b238 Compare November 2, 2026 11:31
… not the array join

Captain verification of the #2608 fix: `${!holders[0]@A}` with
holders=(arr zz) errored `arr zz: invalid variable name` while the
plain `${!holders[0]}` resolved fine.

The word error scan's array-operator arm computed t for EVERY
subscripted indirect source as the dollar_at join of all elements.
That join is GNU's t only for the [@]/[*] subscript, where
array_value runs with AV_ALLOWALL (arrayfunc.c:1563). For a general
subscript, parameter_brace_find_indir / parameter_brace_expand_indir
(subst.c:7839-7885, 7924-7945) resolve the subscripted reference
itself and t is the single element's value, validated before the
operator applies (subst.c:7955-7961).

The scan now splits on the subscript kind: [@]/[*] keeps the join
adjudication verbatim; any other subscript validates t from
array_element_parameter_value on the full reference - the exact
fetch the plain form's scan arm and the plain expansion path
(expand_braced_special.rs) use for a subscripted holder - so
literal, variable, and arithmetic subscripts and assoc keys all
judge the element GNU judges. The value path
(indirect_holder_target_name) already resolved subscripted holders
through that fetch; the scan was the remaining consumer of the
joined scalar.

Probed on GNU bash 5.3.20 with script files (workdir
probes-repair/): sub0_a=[a], sub1_Q=['hi there'], variable /
arithmetic / assoc-key holders all ['hi there'], an unset element
silent; an element of "1bad" under @q, :- and assoc @q tails
reports `1bad: invalid variable name` and aborts the command
(rc=1) in all three shapes. Pins for the whole class added to the
issue2608 stem.

Refs #2608 (PR #2609).
@caomengxuan666
caomengxuan666 marked this pull request as ready for review November 2, 2026 12:05
@caomengxuan666
caomengxuan666 merged commit 4d92522 into master Nov 2, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2608-indirect-positional-transform branch November 3, 2026 09:08
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