Skip to content

[bug] eval 解析错误(未闭合构造)后,后续命令的 CommandNode.line 被冲刷为 eval 行号,ExpansionFailure 同行跳过吞掉脚本剩余部分 #190

Closed

Description

@caomengxuan666

现象(2026-09-26,wt/o1-carrier 调查 rubash#188 时发现)

顶层 eval 'x=${bad'(eval 串内未闭合构造 → 解析错误路径)之后,脚本后续命令全部被跳过,rc=1;GNU 5.3.0 继续执行。

# e7.sh
eval 'x=${bad' 2>/dev/null
echo "e=$?"
x=${.sh.version} 2>/dev/null
echo "m6=$?"
echo end
  • GNU 5.3.0: e=2, m6=1, end
  • rubash: e=2 后即停,rc=1

根因(临时插桩 DBG-ITER/DBG-EXPF 证实)

  • eval 解析错误路径(src/executor/trap_exec.rs execute_eval 的 unclosed_input_close_char_posix 分支)返回后,同脚本 AST 中 eval 之后的所有 CommandNode.line 都变成 eval 所在行号(e7: 全部 Some(1);e9: eval 在第 2 行 → 后续全 Some(2))。
  • 随后 x=${.sh.version} 的 ExpansionFailure 触发 ast_exec.rs 主循环的同行跳过(failed_line == command.line 即跳过),把剩余整个脚本当作同一逻辑行全部丢弃。
  • 对照:eval 串解析成功时(e5: eval 'x=${.sh.version}' 后接同样的 m6)行号正确、脚本继续;无 eval 的 e3 也正确(行号 1,2,3 递增)。
  • 行号冲刷的具体 mutator 未定位(execute_eval 早退路径只触碰 __RUBASH_EVAL_CONTEXT(已还原)与 exit_code);需进一步用 LLDB 或插桩定位是 parse 时还是 runtime 改写。

复现产物

  • target/o1-probes/{e7,e8,e9}.sh(LF 双侧脚本)
  • GNU 对照:e7 → e=2 / m6=1 / end

版本: wt/o1-carrier(c9bd0af6 + #187/#188 修复),对照 WSL GNU Bash 5.3.0(1)-release。

Activity

  1. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    Fixed on wt/p3-comsub (ae54db57, base 26728da) — and the root cause is NOT an eval runtime mutation.

    Actual root cause (parse-time): has_unclosed_parameter_expansion (src/lexer/brace_scan.rs) tracked single-quote state but its ${ branch fired regardless of quote state. A ${ inside a single-quoted word (eval 'x=${bad') therefore kept the logical line open: every later physical line joined into the opening line and inherited its CommandNode.line — the e7/e9 "all lines = eval line" instrumentation is this corruption. The same-line skip then discarded the tail. Minimal reproducer without any eval:

    echo 'x=${bad'
    echo l2
    echo l3

    all three commands got line=Some(1) (DBG instrumentation); GNU 5.3.0 prints the three lines normally, rc 0.

    GNU anchor: parse.y:5305 read_token_word() — single-quoted text is literal until the closing '; ${ there never opens an expansion. Fix: gate the branch with !single (double quotes keep joining — "${unclosed" really is an expansion; ANSI-C already had its own branch).

    Second fix in the same commit: the eval unclosed-construct diagnostic in execute_eval was a raw eprintln! that bypassed eval's own redirections. GNU eval_builtin binds the builtin's redirections before parse_and_execute, so eval 'x=${bad' 2>/dev/null suppresses it. Now routed via write_buffered_builtin_output.

    Verification (WSL GNU Bash 5.3.0): e7 matrix → e=2 / m6=1 / end, rc 0, stdout byte-identical; stderr identical in both the no-redirect (diagnostic printed with eval: line 1 prefix) and 2>/dev/null (suppressed) forms. dstack/dstack2/errors suites unchanged (0/0/2).

  2. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    已在 9ecb59b 落地并关闭(三 commit 已在 issue 评论区有独立说明)。477/477 + 双交叉 + 套件账本零回归。

  3. added
    bug
    area:parser
    on Oct 17, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area:parserbug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions