Skip to content

[continuation] fix: Wave2c P2 batch - arithmetic dollar-brace, export array env leak, tempenv absorption, substring sq operands, raw-byte escapes, dynamic var cells, compound-assignment rc - #573

Merged
caomengxuan666 merged 5 commits into
masterfrom
fix/wave2c-parse-expand-env
Oct 12, 2026

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

八条 P2-medium 解析/展开/环境语义 bug(#533 #534 #538 #539 #540 #541 #542)

每条均先取 GNU bash 5.3.0(WSL stdin/script-file 双法)行为基线并复现存证,修后逐格与 GNU 对齐;一手判定锚定 vendored 源码 third_party/bash/(file:line 见各条)。

#533 算术体内未终止 ${(吞算术闭合符)

#534 export 数组泄漏子进程 env

  • GNU:variables.c make_env_array_from_var_list 对 array_p/assoc_p 单元 continue(ARRAY_EXPORT 未开)——export 属性保留(declare -p 显示 -a -x)但名称不进子 env。
  • 修:setattr/apply.rs 不再把数组 storage 形式写进程 env;child env 组装四处(apply_child_environment / child_shell_environment / exec + printenv 内建回退 / env 内建模拟)统一按 ARRAY_VARS/ASSOC_VARS 标记过滤。

#538 export 前缀赋值过持久 + allexport 无值声明

  • 格1 GNU:setattr.def:590-614 set_var_attribute 只吸收内建操作数具名的 tempenv 变量(FOO=val export BAR 后 FOO unset;具名形态持久),吸收继承 tempenv 属性含 exported(setattr.def:604)。
  • 修:keep-path 选择性还原——非具名条目还原、__RUBASH_EXPORTED_VARS 标记快照不还原(吸收名重打 exported)。
  • 格2 GNU:variables.c bind_variable 在 mark_modified_vars(set -a)期间给一切绑定打 att_exported,含无值 declare。修:declare_local 的 allexport 块覆盖无值操作数。

#539 子串操作数引号 + extquote

  • 格1 GNU:verify_substring_values 把原始操作数文本交 evalexp——单引号 span 是字符串操作数(x: '2': arithmetic syntax error: operand expected rc=1),不像双引号形态被剥除。
  • 格2/3 GNU(huck#8):$'…'-as-name 解码在带引号上下文生效,含子串 offset 与 comsub 体("${x:\{\$'1'}}→abcdef、"$(echo \{$'a'})→zz);顶层无引号仍 bad substitution。
  • 修:parse_parameter_substring_mut 的单引号操作数检查(跳过嵌套替换/$');expand_quoted_parameter_word_mut 入口按上下文(dq/heredoc/comsub 体)先解码再过 bad-name 门;解码有效性放宽到参数名类(位 Diaspora 含位数字/特殊参数,${$$'1'}→$1)。

#540 字节再编码

  • GNU:printf.def:1108-1124 tescape \xNN 为字节转义(*cp = evalue & 0xFF),format 与 %b 两路;strtrans.c ansic_quote 按原始字节流转义。
  • 修:printf/escape.rs 两处 \x 改走 format_escape_byte;declare/storage.rs quote_declare_value 对带 raw-byte 标记的值改为从字节视图引用($'\x81' → $'\201',不再 $'\302\201')。

#541 动态变量

  • 格1 GNU:initialize_dynamic_variables 把动态参数注册为带 dynamic_value 的真实 cell——declare -p RANDOM → declare -i RANDOM="…"(显示时重估),RANDOM/SECONDS/SRANDOM/BASHPID 带 -i,EPOCH*/LINENO/BASH_SUBSHELL/BASH_ARGV0/BASH_COMMAND 为 --,GROUPS 为 -a。
  • 格2 GNU:variables.c:1812 make_funcname_visible——unset FUNCNAME 解绑后函数入口 find_variable 失败不再重建,${FUNCNAME[0]} 保持空。
  • 修:declare -p 的动态视图(execute_with_io_named_in_context 链路传入);FUNCNAME 三个数组读出口补 UNSET_DYNAMIC_VARS 守卫。

#542 复合赋值内 parse 错 rc 2→1

continuation.rs 改动(授权项)

  • unclosed_input_close_char_posix:$[ 推入(]、arith 标记);相邻 \(( 内层推入的 arith 标记(形状推导:report_open && !command == 相邻位);`$"+"{`` 臂对 arith 顶的非 funsub 字面跳过;返回的 array_list 语义扩展为栈内任一 array-list 祖先([exit-status] 复合赋值/数组字面量内 parse 错误退出码:v=($(echo、v=(`echo、v=(" 未终止引号 → rc=2(GNU rc=1,消息行号全对)(P2) #542)。
  • close_char_residuals_advance:上述各臂的停车镜像(K1 重排、$[ 推入、arith 字面跳过、K5 相邻 \(( 未决停车)。
  • comsub_residuals_advance:$[ 与 \))-收口 \(( 的不透明跳过(未决停车+惰性直落)。
  • 对抗探针已覆盖:alias 展开形态(既有 suite 全绿)、多行嵌套 $(( ... $(...) ... ))、heredoc 体、\\-续行(wave2c 既有 heredoc/comsub 回归全绿);\(( 未闭合走 subshell 语义的守护测试(((echo hi); cat 双侧一致)+ brace_scan 单元测试。
  • 凭证:全量 cargo test --no-fail-fast 失败集与 fresh master 逐名双向一致(48/48,无增无减)。

测试证据

  • 新增 tests/issue533*、issue534*、issue538*、issue539*、issue540*、issue541*、issue542*(16 用例,GNU 期望值逐字核对)。RED/GREEN:在 fresh master(1184c960)上除 issue542(master fix(#551): C-family message formats - EOF naming, [[ =~ two-part diagnostic, single-cut stdin line numbers, write-error prefix #563 已修)外全部 RED;本分支全 GREEN。
  • 全量 --no-fail-fast:master 48 失败 vs 本分支 48 失败,逐名双向 diff 为空(同一净化环境:unset WINUXSH_SHELL_PATH_STYLE/WINUXSH_ROOT/BASH_ENV)。
  • 已知残差(不属八条范围,PR 外跟踪):$((…)) 词路径 bad substitution 后的 ExpansionFailure 影响范围是前一条命令(GNU 为整行 list)——z1 形态多弃一条命令;${$$'a'} 顶层无引号的 bad-substitution 消息显示形态(GNU 剥 $ 前缀)属 huck#8 记录的 display residual。

Closes #533, closes #534, closes #538, closes #539, closes #540, closes #541, refs #542(#563 已含其折算,本分支补回归测试)。

…env leak, tempenv absorption, substring sq operands, raw-byte escapes, dynamic var cells

Eight P2-medium parse/expand/env semantic bugs, each WSL GNU bash 5.3.0
verified before and after:

- #533 arithmetic body dollar-brace: an unterminated ${ inside $(( ))/
  $[ ]/(( )) is P_ARITH literal text (parse.y parse_matched_pair), the
  unit's own closer wins at parse time, EOF names )/], and the closed form
  reports expansion-stage bad substitution rc 1 with the script continuing
  (subst.c bad_substitution). Owners: brace_scan param scan arithmetic-unit
  skip, continuation.rs unclosed scanners, parameter_errors + arithmetic
  funnel.
- #534 export arrays: make_env_array_from_var_list skips array/assoc cells
  (ARRAY_EXPORT off) - export/readonly/declare -x no longer publish the
  storage form into the process env, and the child env assembly
  (apply_child_environment / child_shell_environment / exec / env) filters
  array-typed names.
- #538 tempenv absorption: setattr.def set_var_attribute absorbs ONLY the
  operands a builtin names (setattr.def:590-614) - FOO=val export BAR
  leaves FOO unset; absorbed bindings keep the tempenv's exported
  attribute (setattr.def:604). set -a (mark_modified_vars) now also marks
  valueless declare bindings exported (variables.c bind_variable).
- #539 substring operands reach evalexp raw: a single-quoted span is a
  string operand (verify_substring_values), never stripped like the
  double-quoted form; the extquote $'...'-as-name decode applies in
  quoted context including substring offsets and comsub bodies.
- #540 raw-byte escapes: printf \xHH is a byte escape on the format and
  %b paths (printf.def:1108-1124 tescape, *cp = evalue & 0xFF), and
  declare -p quotes marker-carried bytes from the byte view so
  $'\x81' renders $'\201'.
- #541 dynamic variables are registered cells: declare -p renders the
  current dynamic value with the cell attributes (RANDOM/SECONDS/...),
  and unset FUNCNAME stays unbound across function entries
  (variables.c:1812 make_funcname_visible).
- #542 compound-assignment EOF exit family: any construct pending inside
  name=( takes the assignment's rc 1 family (parse.y:7140-7152).

New regression tests: tests/issue53{3,4}*, issue538*, issue539*,
issue540*, issue541*, issue542* (16 cases, GNU-verified expectations).
…rflow read, fatal at the comsub parse level (1 comsub case still red)
… parked overflow in the unclosed-comsub sweep before its own EOF hunt; driver fatal skips unclosed prefixes

The comsub body's tokenize parks HEREDOC_MAX overflow in
HEREDOC_OVERFLOW_LINE, but at execution the body is expanded as a folded
word and the unclosed-comsub sweep (command_execute) reported its own
'residual $(' EOF hunt — 17 gather warnings plus 'unexpected EOF while
looking for matching ')'' — while the parked fatal went unreported.
GNU dies at the 17th push_heredoc with only 'maximum here-document
count exceeded' at the offending line (verified via WSL bash 5.2/5.3
across top-level/comsub/same-line/newline/prefix shapes, all rc 2).
Consume the parked overflow in that sweep ahead of the replay; the
driver-level fatal additionally skips prefixes that are themselves
unclosed (a '$(' whose body carries the offending heredocs never
completed, so re-running its line standalone would emit a phantom
diagnostic ahead of the fatal).

Tests: issue543 suite 12/12 (f2_seventeen_pending_inside_command_
substitution_is_fatal red->green); upstream_suite_slice_snapshots and
the full regression suite 27/27 (the #573 exp red is the ${#RANDOM}
declare -i reseed, already fixed by apply_declare_random_seeds in the
WIP commit); UTF-8 boundary hygiene clean.
@caomengxuan666
caomengxuan666 merged commit 40586ca into master Oct 12, 2026
6 checks passed
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…licitly

command_prepare.rs's function-call redirect gate calls
finish_heredoc_expansion_error for its exit-code store and drops the
run/continue verdict (shape carried over the 32f9260 base on both
sides, unchanged by the #573 merge). The bare call trips unused_must_use
once the crate builds under RUSTFLAGS=-D warnings; make the discard
explicit. No behavior change.
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…are drive-root POSIX form (#564 #565 #566) (#577)

* fix: Wave4c batch - operator-position procsub line joins, empty fnhead newline naming, bare drive root cd (#564/#565/#566)

#564: GNU read_token (parse.y:3776-3796) declines the `<'/`>' operator when
peek_char == '(' and falls into read_token_word, whose shellexp arm
(parse.y:5494-5524) reads the `(...)` body via parse_comsub (parse.y:4451)
across physical newlines - ONE word token regardless of what precedes the
introducer. rubash's group reader committed the logical line after the
introducer line, so `cat <(echo' NL `echo hi' NL `)' died with `syntax
error near unexpected token `(''. Three pieces: the one-shot unclosed-input
oracle (continuation.rs) and the incremental close-char residual scanner
now push the body's `)' matched pair when an ADJACENT `(' follows the
redirect operator (the K5 tail park covers the buffer edge), the join gate
extends to operator-position bodies via the text-level oracle
line_ends_in_open_operator_process_substitution (the exact complement of
the #463 word-attached gate), and the procsub source reconstruction folds
only STRAY joined newlines back to `\n' (leading, or doubled after `;'/`;;'
- positions where a reconstructed `;' is a syntax error a newline never is)
while mid-body and trailing separators keep the `;' spelling the
#373/#381/#390 adjudication pins (`case esac in ; esac', `echo bye ;').
Acceptance: WSL GNU 5.3.0 `cat <(echo hi\necho hi2\n)' runs BOTH lines and
the script continues, `echo <(echo\necho hi2\n)' still evaluates to
/dev/fd/N; 11 new tests green, 4 RED on fresh master d116874.

#565: the shifted `(' of the function_def production (parse.y:1054 via
read_token's parse.y:3774-3790 classification) is the token stream's LAST
token for a final line without a terminator. GNU's reader still hands yacc
the physical line's NEWLINE and rejects THAT: `syntax error near unexpected
token `newline'' + print_offending_line of the `(' line (parse.y:6814;
WSL GNU probes: `[ (` NL, `f (` NL, `echo (` NL and `bash -c '[ ('` all
name `newline', never `('). The name_override arm now maps that
nothing-after-the-shifted-paren shape to `newline' without touching the
#563 C4b neighbours (two+ words still name the `(' itself). `x=1 (' is
pinned as the #452 adjudication boundary, NOT a GNU cell: rubash accepts
assignment-prefix + subshell (real GNU 5.3.0 rejects even the raw corpus
shapes), so the reader's join stands and clean EOF reports the
`unexpected end of file from `(' command' diagnostic at the EOF line.
Harness: the stdin driver binds argv[0] as the diagnostic program name, so
the file normalizes the exe path to `bash' like the #551 harness. 9 tests
green, 4 RED on fresh master.

#566: boundary contract B1 (docs/windows-path-contract.md L1) accepts the
drive-form operands `/c/...', `/c/', `/c/.', `/D' - but the BARE root `/c'
slipped past shell_path_to_windows_inner's drive arm (len >= 3 with a slash
at byte 2) into the shell-root mapping, where map_logical_path resolved it
to <root>/c -> ENOENT: `cd /c' failed while `cd /c/' worked, and a root
carrying a `d/' directory made `cd /d' work by coincidence. The drive arm
now maps the bare root to the drive root (case-insensitive) and
map_logical_path's drive-form exclusion covers the two-letter spelling;
`cd /c' renders the POSIX `/c/' exactly like its slashed sibling, and a
same-named root child no longer shadows the drive. Contract doc ledger and
the suite's "filed separately" note updated. 7 tests green, 5 RED on fresh
master.

* fix(executor): discard the FatalNounset heredoc-expansion verdict explicitly

command_prepare.rs's function-call redirect gate calls
finish_heredoc_expansion_error for its exit-code store and drops the
run/continue verdict (shape carried over the 32f9260 base on both
sides, unchanged by the #573 merge). The bare call trips unused_must_use
once the crate builds under RUSTFLAGS=-D warnings; make the discard
explicit. No behavior change.
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…cise

Master's #573 wave flipped the two dparen EOF arms in run_source_impl
(dparen_arith_group_never_closes, dparen_subshell_reparse_compound_eof)
to a dual-char `) [' wording. GNU's EOF reports carry a single char:
parse.y:3912 prints the matched-pair close char, parse.y:7152 the
literal )' - a `) [' report has no GNU oracle, and no test on either
side pins it, while the #390/#413 wording pins (issue390 q1 family,
issue413 dparen_verdict_arithmetic_side_stands) exercise `)' and pass
on the pre-merge branch. Restore the `)' wording in the merge so the
branch's dparen-verdict semantics (#543 a1/a3: the dparen family owns
the )' verdict) survive master's rewording; master's other #573 changes
in the region are untouched.
caomengxuan666 added a commit that referenced this pull request Oct 13, 2026
…rap list/DEBUG freeze, bash -o listing, jobs/alias params (#543 #544 #545 #546 #547 #549) (#578)

* WIP(wave4b): #543 dollar-bracket EOF scan + #544 declare -aA conflict (checkpoint before master merge)

* WIP(wave4b): #545 procsub lifetime+xtrace, #546 trap cells, #547 -o listing, #549 jobs/alias (checkpoint 2)

* fix(wave4b): #545 procsub fd lifetime + [[ ]] operand realization/xtrace, #546 trap listing/BASH_COMMAND freeze, #547 bash -o list form, #549 jobs lenient specs + alias -p whole-table, #544 -aA tests, #543 locks

* fix: discard best-effort heredoc-expansion-error Result in function-call redirect rejection arm

RUSTFLAGS=-D warnings surfaced an unused-must-use on the FatalNounset
arm's finish_heredoc_expansion_error call (present on master 40586ca
as a bare statement). The command's result is decided by the rejection
status after the temporary-assignment restore, so the materialization
Result is deliberately discarded - mirror the let _ = contract the
posix-assignment-failure arm already uses for its finish calls.

* test(#545): pin scalar-assignment stored procsub path as dead after the creating command

part_080's assignment input-procsub test still pinned the pre-#545
contract (stored /dev/fd/N stays readable, cat prints alpha). The fd
lifetime fix reaps a command's substitutions when the creating command
finishes (GNU execute_cmd.c:832 delete_procsubs closes fds or unlinks
fifos; WSL GNU 5.3.0(1) probe 2026-10-12), and the array form of the
same contract is already pinned in issue545_procsub_lifetime_xtrace_
regressions. Flip the scalar form the same way part_072's operand
matrix was flipped: cat reports ENOENT rc 1, redirect target stays
empty. Same-family flips in this PR: part_035 (#549), part_072 (#545).

* fix(#390): keep the single-char dparen EOF wording the #390 pins exercise

Master's #573 wave flipped the two dparen EOF arms in run_source_impl
(dparen_arith_group_never_closes, dparen_subshell_reparse_compound_eof)
to a dual-char `) [' wording. GNU's EOF reports carry a single char:
parse.y:3912 prints the matched-pair close char, parse.y:7152 the
literal )' - a `) [' report has no GNU oracle, and no test on either
side pins it, while the #390/#413 wording pins (issue390 q1 family,
issue413 dparen_verdict_arithmetic_side_stands) exercise `)' and pass
on the pre-merge branch. Restore the `)' wording in the merge so the
branch's dparen-verdict semantics (#543 a1/a3: the dparen family owns
the )' verdict) survive master's rewording; master's other #573 changes
in the region are untouched.
@caomengxuan666
caomengxuan666 deleted the fix/wave2c-parse-expand-env branch October 13, 2026 02:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment