Skip to content

fix: Wave2a shell semantics batch - SIGQUIT policy ignore, -r entry via -s, readonly attr refusal, declare -na shape wins, ERR single-fire guard - #560

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/wave2a-shell-semantics
Oct 12, 2026
Merged

caomengxuan666 merged 5 commits into
masterfrom
fix/wave2a-shell-semantics

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Wave2a batch: five P2-medium shell behavior semantics bugs from the 443-divergence audit, each with a GNU oracle verification (WSL bash 5.3.0, 2026-10-12), an independent commit, and a RED-then-GREEN regression suite under tests/issueNNN_*.rs. Behavior contracts are anchored on the vendored bash source (third_party/bash, bash-5.3-16); tests and comments describe behavior only.

Issue Root cause Fix Tests
#526 non-interactive SIGQUIT kills the shell rc 131 The Windows signal-mailbox dispatch treated an untrapped SIGQUIT delivery as a killing default; GNU sets SIGQUIT to ignore in every shell (sig.c:333) and trap.c marks it SIG_SPECIAL for resets Both dispatch sites (run_pending_signal_traps, observe_signals_while_blocked) drop a QUIT with no trap action; new platform SIGQUIT_NUMBER const. Trapped QUIT still fires; SIGINT/SIGTERM stay fatal 7 tests; 4 red on master
#527 -r only restricted when it precedes -s The -s arm ran the stdin script immediately, so a trailing -r was swallowed as a positional ($1=-r) -s now only marks read-from-stdin and the argv scan continues (shell.c parse_short_options model); non-option words after -s become positionals, never a script file; stdin run moves after the loop behind the -c usage check 5 tests; 2 red on master
#528 readonly variable attribute changes silently accepted The declare attribute pass had no readonly gate for the set direction: declare -i R on readonly R produced -ir Per-operand gate in declare's operand loop: invalid readonly attrs (integer/array/assoc/case-map, either direction) refused with one readonly variable diagnostic; assignments paired with any attr apply nothing; export/trace/-r/+x/-p stay allowed (variables.h valid/invalid_readonly_attrs, declare.def refusal order) 15 tests; 7 red on master
#530 declare -na kept -n The nameref attribute was marked even when an array shape was requested in the same command, so later assignments routed through nameref indirection (r=5 failed nameref validation; declare -na rv=tgt; tgt=zz wrote through the reference) The shape wins: the nameref mark is cleared for the command when -a/-A rides along - silent at global scope, one reference variable cannot be an array diagnostic at function scope with the plain local array still created; existing arrays keep the conflict diagnostic (invalid-value check no longer masks it for array cells); chain-carrying namerefs convert in place without chain-follow 16 tests; 13 red on master
#531 ERR trap fired twice for failures in { }/for/case bodies Fixed en route by the #507/#557 errexit body adjudication (the post-execute_command gate orders both the ERR fire and errexit promotion per body) No code change needed - guard suite locks the contract; verified 3/8 red on the audited baseline 764b9dd (E×5 era), 8/8 green here 8 tests; 3 red on 764b9dd, 0 red here

Test plan

  • Every issue suite verified RED before the fix and GREEN after (except [trap] ERR 于 { }/for/case 体内失败触发两次:E×5 vs GNU E×3 (P2) #531, whose RED proof was taken on a 764b9dd worktree build since master already conforms).
  • GNU oracles run on WSL bash 5.3.0 for every expectation written into the tests, including the corner matrices (readonly attr refusal order, -na conflict precedence, ERR no-fire contexts like AND-OR left arms / negated pipelines / redirected brace groups, errtrace double fire for a failing function).
  • Full cargo test --no-fail-fast on this branch vs pristine origin/master (db3218a): failure sets identical name-by-name, 53 = 53, zero new failures, zero masked failures. cargo fmt --check clean.

…28+SIGQUIT (#526)

kill -QUIT $$ in a non-interactive shell ended the whole rubash process
with rc 131 and skipped the rest of the script. GNU bash ignores SIGQUIT
in every shell: sig.c:102 initialize_signals -> sig.c:230
initialize_shell_signals sets SIGQUIT to SIG_IGN (sig.c:333) before the
interactive-only SIGINT/SIGTERM installs, and trap.c:187-188 marks SIGQUIT
SIG_SPECIAL so a trap reset returns to that ignore. WSL bash 5.3.0 oracle:
'kill -QUIT $$; echo survived=$?' prints survived=0 rc 0, also under
'bash -c' and after 'trap - QUIT'.

The Windows mailbox dispatch treated an untrapped delivery as a killing
default. Both dispatch sites now drop a QUIT with no trap action:
run_pending_signal_traps (command boundary) and
observe_signals_while_blocked (external-wait/sleep observer), keyed on a
new platform SIGQUIT_NUMBER const beside SIGCHLD_NUMBER. Trapped QUIT
still runs the action; untrapped SIGINT/SIGTERM keep their killing
default (pinned by guard tests: rc 130/143). The unix kernel backend
already encodes the same policy ignore, so its dispatch path is inert.

Tests tests/issue526_sigquit_noninteractive_ignore.rs: kill-self via -c
and -s, delivery while blocked in an external wait, trap-reset ignore,
trapped action still fires, SIGINT/SIGTERM still fatal. 4/7 red on
master, green here; GNU oracle WSL 5.3.0 2026-10-12.
… the argv scan (#527)

'bash -s -r' ran the stdin script the moment -s was seen, so the trailing
-r was silently swallowed as a positional parameter: the shell came up
unrestricted (flags=hBs, cd allowed) with $1=-r. The first entry path
('bash -r -s') already restricted, leaving the two documented call shapes
disagreeing. GNU scans argv left to right: -s only marks read_from_stdin
(shell.c parse_short_options), remaining options still apply, and
non-option words after -s become positional parameters - a word that
looks like a script path stays a positional too, stdin remains the
source (verified WSL bash 5.3.0: 'bash -s /no/such/file' reads stdin,
$1=the word; 'bash -s -r a b' -> flags=hrBs, $1=a $2=b).

The -s arm now records read_from_stdin and the scan continues; non-option
words collect as positionals (skipped as script files); after the loop
the stdin script runs with those positionals, after the pending -c usage
check so -c keeps precedence. -r -s, plain -s positionals, and -r -c are
pinned against regressions.

Tests tests/issue527_restricted_shell_entry.rs: -s -r restricts (cd
refused rc1, flags=hrBs), -r -s unchanged, -s -r a b positionals, plain
-s a b unrestricted, script-looking word after -s still reads stdin.
2/5 red on master, green here; GNU oracle WSL 5.3.0 2026-10-12.
'readonly R=1; declare -i R' silently turned R into -ir: the attribute
pass had no readonly gate for the set direction, so third-party code
could inject integer/array/assoc/case-mapping attributes onto a readonly
variable and silently weaken the guarantee. GNU protects the attributes
with the value: declare.def:893 refuses setting or clearing any of the
invalid readonly attributes (integer/array/assoc/upper/lower/capcase,
variables.h:126-128 valid/invalid_readonly_attrs) with one
'<cmd>: <name>: readonly variable' diagnostic and a per-operand skip,
declare.def:881 leaves an assignment word with nothing applied, and the
refusal is all-or-nothing for mixed requests (-ix applies neither).

The gate sits in declare's operand loop before assignment or attribute
application, keyed on the existing readonly marks: invalid attrs in
either direction, or an assignment paired with any modifiable attribute,
error and skip the operand. Export, trace, re-setting readonly, +x and
-p display stay allowed (rc 0, GNU-verified); +r removal, local -i, and
plain assignments keep their existing single-diagnostic refusals.

Tests tests/issue528_readonly_attr_change_refused.rs: -i/-a/-u/+i
refusals with value and attributes preserved, -ir re-request, mixed -ix,
assignment+attr single diagnostic, allowed export/trace/-r/-p paths,
function-scope single diagnostic, and the pre-existing +r/local/assign
guards. 7/15 red on master, green here; GNU oracle WSL 5.3.0
2026-10-12.
…together (#530)

'declare -na r' kept the nameref attribute ('declare -an r'), so every
later assignment and expansion routed through nameref indirection instead
of storing into the array - 'r=5' after the declaration even failed
nameref value validation, and 'declare -na rv=tgt; tgt=zz' wrote through
the reference. GNU treats nameref and array/assoc shapes as mutually
exclusive with the shape winning: the nameref bit is lifted out of the
set mask and cleared whenever the variable is or becomes an array
(declare.def:962-981), an existing array conflicts with the -n request
(declare.def:841), and the chain transform stays out of commands that
toggle -n (declare.def:206). WSL bash 5.3.0 oracle: global -na/-nA is a
silent drop (rc 0, 'declare -a r'); function scope still reports
'<cmd>: <name>: reference variable cannot be an array' once while
leaving the plain local array behind; an existing scalar moves to
element 0; a valueless or chain-carrying nameref converts with its cell
kept as element 0.

The operand loop now clears the nameref mark for the command when -a/-A
rides along - silent at global scope, one diagnostic at function scope -
and the marks plus the assignment routing run with the lifted bit
(nameref_marks). Existing arrays keep the declare.def:841 conflict with
its proper message (the invalid-nameref-value check no longer masks it
for array/assoc cells), a chain-carrying nameref under -na converts in
place without following the chain, and the readonly, invalid-value and
compound-assignment refusals keep their single-diagnostics.

Tests tests/issue530_declare_na_drops_nameref.rs: global/function drops,
-nA, scalar and nameref conversions, existing array/assoc conflicts,
assignment routing before and after the declaration, invalid-value and
readonly orderings, compound guard. 13/16 red on master, green here;
GNU oracle WSL 5.3.0 2026-10-12.
… conforms

'trap 'echo E' ERR; { false; }; for i in 1; do false; done; case x in
x) false;; esac' printed E five times (each compound body re-adjudicated
its aggregate status after the inner failing command had already fired
the trap once). Master now prints E three times: the errexit body
adjudication from the P1 semantic batch gates BOTH consumers at the
post-execute_command site for if/while/until/for/case/select bodies, so
the ERR fire - whose GNU adjudication point is once per command in
execute_command_internal (execute_cmd.c:851 was_error_trap family, with
compound bodies handing their status up without a second look) - no
longer doubles.

No code change accompanies this commit: 3/8 of these tests are red on
the audited baseline 764b9dd (verified on a worktree build: the repro
prints E x5, the construct matrix carries the extra body fires, the
nested for-in-case double-fires) and 8/8 are green on this branch. The
tests pin the whole GNU-verified contract so the double fire cannot
return: one fire per failing body command; no fire for if/while/until
conditions, negated pipelines, redirected brace groups, or AND-OR left
arms; one fire for the final failing AND-OR arm; one fire for the failed
command-substitution assignment; function bodies silent without
errtrace but the failed call firing once (twice with set -E - body plus
call, GNU behavior); one fire on the set -e exit path.

GNU oracle WSL bash 5.3.0, 2026-10-12: every expected line sequence in
this file was captured from it; rubash matches byte for byte.
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