Skip to content

fix(expand): unset scalar base has no element 0 for operators - #910

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/unset-scalar-index-zero
Oct 20, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/unset-scalar-index-zero

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Problem

array_element_parameter_value returned Some("") for index 0 of an unset variable, so the parameter looked set-but-empty to the operators:

unset Z
echo "${Z[0]-d}"   # got: empty, GNU: d
echo "${Z[0]+a}"   # got: a,     GNU: empty
: "${Z[0]=d}"; echo ""  # got: empty (never assigned), GNU: d

Only :- behaved (it treats null like unset too). A retired coproc name unbinds its array, so this also broke c_command_retires_finished_coproc_endpoints_before_later_redirects (c0=<> instead of c0=<unset>).

Fix (src/executor/arrays/executor.rs)

In the scalar-base arm, report the reference as unset (None) when there is no storage and no live dynamic value; a set-but-empty scalar keeps its empty element 0. A dynamic scalar (RANDOM/SECONDS/LINENO...) reads live through element 0, shadowing stored seed text — the same dynamic-first precedence plain $name expansion uses. Probed against GNU bash 5.3 for -/:-/=/+ on unset, set-empty and dynamic bases, plus the plain ${Z[0]} case (still empty).

Tests

  • New: cli_tests::unset_scalar_base_index_zero_is_unset_for_operators.
  • cargo test --locked --test cli_tests: 466 passed, 0 failed (was 464 passed / 1 failed).
  • cargo test --locked --test executor_tests: 1541 passed / 11 failed vs 1540 / 12 on the pristine-HEAD baseline (worktree, same device) — one extra fix (part_071 shellmath_decimal_divide), zero new failures. The 11 remaining fail identically at baseline: Android/Bionic signal numbering (kill/trap), ulimit text, Termux // PWD prefix — all platform environment, none touch this path.
  • Changed files are rustfmt-clean (printf/number.rs has a pre-existing fmt diff at HEAD; untouched here).

array_element_parameter_value returned Some("") for index 0 of an
unset variable, so ${Z[0]-d},${Z[0]+a},${Z[0]=d} all misread the
parameter as set-but-empty (only :-, which also treats null as
missing, behaved). Report the reference as unset (None) instead —
the same posture as any other missing element — while a set-but-empty
scalar keeps its empty element 0.

A dynamic scalar (RANDOM/SECONDS/...) reads live through element 0,
shadowing stored seed text: the same dynamic-first precedence plain
$name expansion uses.

Verified against GNU bash 5.3 for -/:-/=/+ on unset, set-empty and
dynamic bases, plus the plain ${Z[0]} empty case. Fixes
cli_tests::c_command_retires_finished_coproc_endpoints_before_later_redirects
(retired coproc names unbind, so element 0 must read unset) and one
executor_tests shellmath case; no new failures vs the pre-change
baseline on aarch64-linux-android.
@caomengxuan666
caomengxuan666 merged commit f2cc96a into master Oct 20, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/unset-scalar-index-zero branch October 20, 2026 08:04
caomengxuan666 added a commit to jhermelink/rubash that referenced this pull request Nov 5, 2026
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