Repository navigation
WIP: fix #2486 — heredoc $ escape under set -u (nounset scan input) + bg-subshell assoc element assignment (respawn attribute transport) - #2491
Conversation
Face A of rubash#2486: under set -u an unquoted heredoc body's $ was reported as an unbound reference (\$PREFIX -> 'PREFIX: unbound variable') and killed the script, while set +u printed the correct literal text. GNU: redir.c:348 heredoc_expand() expands an unquoted body with expand_string_to_string(word, Q_HERE_DOCUMENT) (redir.c:374). In expand_word_internal (subst.c:11229) the backslash branch (subst.c:11614-11661) consumes the escape atomically BEFORE any $ dispatch: under Q_HERE_DOCUMENT the escape class is CBSHDOC = backslash,backtick,dollar (syntax.h:27 slashify_in_here_document, syntax.h:59; tflag at subst.c:11628-11629) and the escaped $/` become CTLESC-protected data (subst.c:11659-11661) - parameter expansion, and with it the nounset verdict, never runs on them. Root cause: parameter_expansion_error_in_heredoc_body scanned the RAW body, but the real expansion path (expand_heredoc_body_mut) scans prepare_unquoted_heredoc_expansion output where $ is already DATA_DOLLAR (which the nounset scan skips). The verdict and the expansion disagreed by construction. Fix: run the heredoc error scan on the same prepared text (one owner of the escape class: $ -> DATA_DOLLAR, ` -> DATA_BACKTICK, \ -> DATA_BACKSLASH), so a collapsed backslash pair before $set still expands (and can still be unbound) while $ never opens an expansion. No per-symptom guard. Pins: tests/issue2486_heredoc_escaped_dollar.rs (installer shape, escape class x set +u/-u, quoted delimiter, real-unbound still fires). Verified against WSL GNU bash 5.3.20 script files byte-for-byte. Note: committed with --no-verify because 'cargo fmt' fails with os error 206 on this machine (fails in the pristine main checkout too); the hook's check was substituted by per-file 'rustfmt --edition 2021 --check' over every tracked crate file - zero diffs.
Face B of rubash#2486: under set -u, an associative-array element assignment inside a BACKGROUND subshell reported the subscript as an unbound variable (building[$1]=1 with $1=app -> 'app: unbound variable'), while the same call at top level and in a plain ( ... ) subshell worked. Even a literal building[appx]=1 with no $ misfired. GNU: the async child is a fork (execute_cmd.c:1761-1763 execute_in_subshell) and inherits the attribute table whole. For an assoc array the subscript of name[sub]=value is WORD-expanded (arrayfunc.c:392-408 assign_array_element_internal -> expand_subscript_string at arrayfunc.c:396 / subst.c:11063) and used literally as the key (arrayfunc.c:408 bind_assoc_variable) - the expanded key is never re-read as a variable reference, so nounset can never fire on it. Only INDEXED arrays evaluate the subscript arithmetically (arrayfunc.c:425 array_expand_index -> evalexp), where a bare unset name under set -u IS fatal - that contrast behavior is kept and pinned. Root cause: the ( ... ) & body is a respawned 'rubash -c' child, and its env transport iterated the write-behind VarTable directly (#916), so the __RUBASH_*_VARS attribute markers were never materialized: 'declare -A building=()' crossed as a plain string, the child treated building as INDEXED, the subscript took the arithmetic path, and 'app' raised nounset. Fix: all four respawn-env assembly sites (cmd & wrapper, coproc, external_setup respawn, input procsub child) now iterate env_vars_snapshot(), the sanctioned renderer that materializes every attribute key from the structured flags (public_accessors.rs:1437, 'child-env bootstrapping reads them'). Bisect evidence: top-level OK, plain subshell OK, only '&' children failed; declare -p inside the bg child showed 'declare -x building="()"' pre-fix and 'declare -Ax building=()' post-fix. The residual -x (every transported shell var rides as an env pair) is a pre-existing respawn-transport artifact for all unexported vars, out of scope here. Pins: tests/issue2486_assoc_assign_subshell_nounset.rs (worker shape x top/plain-subshell/bg, literal subscript, bg declare -A, set +u control, indexed-arith contrast). Repro + matrix verified against WSL GNU bash 5.3.20 script files byte-for-byte. Note: committed with --no-verify because 'cargo fmt' fails with os error 206 on this machine (pristine main checkout too); substituted by per-file 'rustfmt --edition 2021 --check' - my edited regions are clean; two pre-existing drift spots on untouched lines (compound_exec.rs:4065, trap_exec.rs:387) come from upstream e6ccbf1 and are left alone.
22d78a5 to
86c15c4
Compare
|
[win-acceptance] #2491 PASS — both new pins green at --test-threads=4 (heredoc_escaped_dollar 4/4, assoc_assign_subshell_nounset 5/5 x2); both issue #2486 repro scripts (t8 evidence originals) byte-identical to WSL GNU Bash 5.3.20 — face A stdout/stderr 0 diff, rc 0; face B stdout/stderr/log1.txt side file 0 diff, rc 0. Environment: Windows 10 19045 real machine, MSVC Rust debug build of master 2506da1 (includes this merge, parent step d269fb0), GNU oracle = Family battery at -j4 (19 binaries): heredoc_regressions 3/3, nounset families 1130 4/4, 1149-50 2/2, 1168-69 2/2, 1177 3/3, 200 2/2, array_store_parity 2/2, assoc families 1297 1/1, 1298 1/1, 1311 2/2, 1677 12/12, bg/coproc/procsub families 1074 4/4, 1408 3/3, 1220 2/2, 1223 3/3, 1336 5/5, 1460 1/1, 1370 4/4 — all green. issue1930 has 1 red of 3 = the tempdir-path-prefix environment shape this PR's body already documented as pre-existing on clean master ( The bg-subshell respawn attribute transport (face B) is a Windows cfg-face fix and it is verified end-to-end here: the No BLOCKER. |
WIP: two independent
set -ufaces of #2486. Fixes verified against WSL GNU bash 5.3.20 script files; not ready to merge until reviewed.Refs #2486
Face A — unquoted heredoc
\$underset -u(commit 5f8e3b8)GNU anchor
redir.c:348 heredoc_expand()— an unquoted heredoc body goes throughexpand_string_to_string (redirectee->word, Q_HERE_DOCUMENT)(redir.c:374).subst.c:11229 expand_word_internal(), backslash branchsubst.c:11614-11661:\xis consumed atomically BEFORE any$dispatch. UnderQ_HERE_DOCUMENTthe escape class isCBSHDOC=\/```` become CTLESC-protected data (subst.c:11659-11661) — parameter expansion, and with it the nounset verdict, never runs on them.Bisect conclusion
set +uwas already byte-correct (literal${0%/*}/$PREFIXprinted), so the heredoc escape layer was NOT wrong; only the unbound check saw the escaped$. Root cause:parameter_expansion_error_in_heredoc_bodyscanned the RAW body, while the real expansion path (expand_heredoc_body_mut) scansprepare_unquoted_heredoc_expansionoutput where\$is alreadyDATA_DOLLAR(which the nounset scan skips). Verdict and expansion disagreed by construction.Change
src/executor/parameter_errors.rs— run the heredoc error scan on the same prepared text (prepare_unquoted_heredoc_expansion, madepub(in crate::executor)insrc/executor/command_input_scope.rs). One owner of the whole escape class (\$→DATA_DOLLAR,\``→DATA_BACKTICK,\→DATA_BACKSLASH):\$setstill expands (and can still be unbound),$` never opens an expansion. No per-symptom guard.Face B — bg subshell assoc element assignment under
set -u(commit 22d78a5)GNU anchor
execute_cmd.c:1761-1763 execute_in_subshell— the async child is a fork; it inherits the variable-attribute table whole.arrayfunc.c:392-408 assign_array_element_internal— for an ASSOC array the subscript is word-expanded (expand_subscript_string,arrayfunc.c:396/subst.c:11063) and used literally as the key (bind_assoc_variable,arrayfunc.c:408); the expanded key is never re-read as a variable reference, so nounset can never fire on it.arrayfunc.c:425 array_expand_index— INDEXED subscripts are arithmetic; a bare unset name there IS fatal underset -u(expr.c). Both shells agree on that shape; it is pinned, not silenced.Bisect conclusion
Top-level OK, plain
( ... )OK,$( ... )OK — only( ... ) &children failed, and even a literalbuilding[appx]=1with no$misfired.declare -p buildinginside the bg child printeddeclare -x building="()"(plain string!) while a plain subshell printeddeclare -A building=(). Root cause: the( ... ) &body is a respawnedrubash -cchild whose env transport iterated the write-behind VarTable directly, so the__RUBASH_*_VARSattribute markers were never materialized; the child treatedbuildingas INDEXED, the subscript took the arithmetic path, andappraised nounset.Change
All four respawn-env assembly sites now iterate
env_vars_snapshot()— the sanctioned renderer that materializes every attribute key from the structured flags (public_accessors.rs:1437, "child-env bootstrapping reads them"):src/executor/compound_exec.rs—cmd &wrapper + coproc respawnsrc/executor/external_setup.rs— respawn transportsrc/executor/trap_exec.rs— input procsub childPost-fix the bg child prints
declare -Ax building=(); the residual-x(every transported shell var rides as an env pair, so the child sees it as exported) is a pre-existing respawn-transport artifact affecting ALL unexported vars, out of #2486's scope.Verification
Oracle:
MSYS_NO_PATHCONV=1 wsl bash <script file>(GNU bash 5.3.20, /usr/local/bin/bash), stdout/stderr captured separately; basee6ccbf14.\$/\${x}/\\\$/\\$set/plain$set/quoted-delimiter ×set +u/set -u; B: top/plain-subshell/bg × fn-call/literal-subscript, indexed-arith contrast) byte-identical to WSL (path-prefix in stderr aside, which is environmental).tests/issue2486_heredoc_escaped_dollar.rs(4),tests/issue2486_assoc_assign_subshell_nounset.rs(5) — all green.\r\n, issue1981, issue1153cpresolution, issue2134, issue1219, issue1930 tempdir-path prefix) — verified by stash-and-run on the clean tree.cargo test --lib: 1124 passed / 2 failed — identical counts to clean master (pre-existing, path-shape sensitive).Disclosure: rustfmt / pre-commit hook
cargo fmtfails withos error 206(filename too long) on this machine — including on the pristine main checkout, so the pre-commit hook (which shells out tocargo fmt --check) cannot pass machine-wide and commits were made with--no-verify. Substituted check: per-filerustfmt --edition 2021 --checkover every tracked crate file — my edited regions are clean. Two pre-existing drift spots on lines I do not touch (compound_exec.rs:4065,trap_exec.rs:387) come from upstreame6ccbf14and were left alone.Process note
During verification, a sibling session's
git stash popin another worktree consumed this branch's uncommitted face-B stash (stashes are shared across worktrees in this repo). Face B was re-applied from known diffs, re-verified byte-for-byte against the WSL oracle, and committed; the foreign WIP that landed in this worktree was reverted without pushing anywhere.