Skip to content

fix(lexer,executor): #880 close mark_quoted_ifs_from_raw bail blind spots - #898

Closed
arookieofc wants to merge 1 commit into
masterfrom
arookieofc/fix-880-mark-quoted-ifs-walk
Closed

arookieofc wants to merge 1 commit into
masterfrom
arookieofc/fix-880-mark-quoted-ifs-walk

Conversation

@arookieofc

Copy link
Copy Markdown
Member

Closes #880. mark_quoted_ifs_from_raw Bailund spots fixed via direction A (complete the walks); mismatch still bails (safe direction).

GNU basis: parse.y:4451 parse_comsub + parse.y:3465-3468 special_case_tokens; subst.c:921-931 string_extract_double_quoted stripdq arm.

  • dquote-wrapped backtick \" folds to ": the chars-iterator backtick copier gains a dq flag and the dquote walker passes dq=true (outer backticks unchanged). The simulator's dquote backtick arm replays the same fold plus \<newline> elision.
  • backtick \<newline> / \<CRLF> elision completed in the simulator.
  • $(...) is now case-aware, arm-for-arm with copy_dollar_paren_body_raw, reusing the shared update_command_substitution_case_depth machine (parse.y:3710/3759, 3181/3183, 3630-3643); quote/comment/nested-$() spans are skipped opaquely (function widened to pub(crate) with a lexer-level re-export).

Evidence: target/probe/880r.sh + 880s.sh + 880t.sh + 880u.sh byte-match WSL GNU Bash 5.3.15; new tests/issue880_comsub_case_ifs_mark_regressions.rs 6/6; #868 7/7 + #876 7/7; regression 78/78; lib 706 pass + 1 pre-existing env-flaky windows_listing_cache (fails on clean master too).

… bail blind spots

GNU parse.y:4451 parse_comsub re-parses the $(...) body with the real
grammar, so a case clause owns its pattern `)` (parse.y:3465-3468
special_case_tokens); and subst.c:921-931 string_extract_double_quoted
folds \" to a bare `"` while scanning the enclosing quotes (stripdq arm).

The IFS-marking simulator replayed neither rule, and replayed no backtick
line-continuation rule either, so any word pairing a quoted IFS char with
one of these shapes desynced the simulated cook, hit the equality-guard
bail, and split its quoted IFS:

- dquote-wrapped backtick kept `\"` verbatim while the cook folds it
  (`a="`echo \"x\"`"` gave `"x"` for `x`); the chars-iterator backtick
  copier gains the dq flag and the dquote walker passes dq=true
  (outer backticks unchanged). The simulator's dquote backtick arm
  replays the same fold plus `\<newline>` elision.
- backtick `\<newline>` / `\<CRLF>` elision was not replayed in the
  simulator's dquote backtick arm.
- `$(...)` used a naive paren depth count that closed early on
  `$(case x in ...)` pattern parens; the tail was re-walked as outer
  text, so a `\`/`"`/`'` there desynced the cook and split quoted IFS
  (e.g. `"a b"$(case x in x) echo \$y;; esac)` gave `a` + `b$y`).
  Now case-aware, arm-for-arm with copy_dollar_paren_body_raw via the
  shared update_command_substitution_case_depth machine (parse.y:3710/
  3759, 3181/3183, 3630-3643; rubash#276/#284/#380/#513), with opaque
  quote/comment/nested-$() span skips (widened to pub(crate) with a
  lexer-level re-export).

Mismatch still bails (safe direction); fixed shapes no longer bail.

Evidence: target/probe/880r.sh + 880s.sh + 880t.sh + 880u.sh byte-match
WSL GNU Bash 5.3.15; new tests/issue880_comsub_case_ifs_mark_regressions.rs
6/6; #868 7/7 + #876 7/7; regression 78/78; lib 706 + 1 pre-existing
env-flaky windows_listing_cache (fails on clean master too).
@github-actions

Copy link
Copy Markdown
Contributor

欢迎!这是你的第一个 PR 🎉 请确认已跑过本地测试套件;CI 通过后维护者会 review。

@caomengxuan666

Copy link
Copy Markdown
Member

Code Review (80aea329) — REQUEST-CHANGES

整体方向(按 lexer/quotes.rs 权威实现补齐 mark_quoted_ifs_from_raw 的模拟器 walk,复用 lexer::skip::update_command_substitution_case_depth)符合 AGENTS.md(#117 收敛权威状态机、不堆黑名单症状守卫)与 GNU parse.y:4451 / parse.y:3465-3468 / subst.c:921-931。tests/issue880_comsub_case_ifs_mark_regressions.rs 中的 $(case ...) 4 个探针(Face 3a–3d)均已对位。

但在 src/executor/command_prepare.rs 的反引号模拟分支中存在 3 处可复现的 bug(其中 2 处在 WSL GNU Bash 5.3.0 下直接复现 simulated != cooked 触发 bail、打散引号内空白):


1. [Blocker] Face 2(双引号内反引号 \" 折叠)在模拟器中推入了 DATA_DQUOTE(\x18)而非裸 ",导致命令词路径 100% 触发 bail

  • 位置:src/executor/command_prepare.rs:4641-4648(if ch == '' && !in_single && in_double`)
  • 原因:
    权威 lexer 路径 copy_backtick_body_cursor(out, rest, true)(src/lexer/quotes.rs:700-703)以及本 PR 新增的 copy_backtick_body_preserving_syntax_dq(..., true)(src/lexer/quotes.rs:1624-1626)在 dq=true 下将反引号体内的 \" 折叠为裸 "(ASCII 0x22)。
    但 mark_quoted_ifs_from_raw 的 in_double 反引号分支在 src/executor/command_prepare.rs:4645 写成了:
    '"' => {
        simulated.push(crate::executor::markers::DATA_DQUOTE);
        index += 2;
        continue;
    }
    这导致 cooked 中是裸 ",而 simulated 中是 \u{18}(DATA_DQUOTE),在 command_prepare.rs:4746 的 if simulated != cooked 比较中必定不等并直接 bail。
  • 为何现有测试未捕获:
    tests/issue880_comsub_case_ifs_mark_regressions.rs:68-73(dquote_wrapped_backtick_escaped_quote_folds)测的是纯赋值词 a="\echo "x"`",它走的是 lexer/word.rs:287的remove_shell_quotes_outside_backticks,根本不会进入 mark_quoted_ifs_from_raw`。
  • 复现(WSL GNU Bash 5.3.0 对拍):
    w="p q"
    printf "<%s>\n" "a b`echo \"x\"`"$w
    • WSL GNU Bash 5.3.0:
      <a bxp>
      <q>
      
    • 本 PR (80aea329):因 \x18 != '"' 触发 bail,"a b..." 中的受保护空格丢失标记被拆词:
      <a>
      <bxp>
      <q>
      
  • 建议修法:将 src/executor/command_prepare.rs:4645 的 simulated.push(crate::executor::markers::DATA_DQUOTE); 改为 simulated.push('"');,并在 tests/issue880_comsub_case_ifs_mark_regressions.rs 中补充上述带未引用展开的命令词回归测试。

2. [Blocker] copy_comsub_body_verbatim_for_ifs_marking 内反引号分支的 _ => 臂只前进了 1 个字符,导致 $() 内反引号中的 ``` 提前闭合反引号

  • 位置:src/executor/command_prepare.rs:176-209(copy_comsub_body_verbatim_for_ifs_marking 的 '`' 分支)
  • 原因:
    权威实现 copy_backtick_body_cursor(src/lexer/quotes.rs:707-722)与 copy_backtick_body_preserving_syntax_dq(src/lexer/quotes.rs:1627-1631)在遇到 \ + 非换行字符 escaped(如 \`` 或 \)时,会将 ` 和 escaped 成对推入并前进 2 个字符,从而保证转义的 \`` 不会闭合反引号、转义的 \不会把第二个` 当成新的转义符。
    但在 copy_comsub_body_verbatim_for_ifs_marking(command_prepare.rs:188-206)中:
    if c == '\\' {
        match raw_chars.get(*index + 1).copied() {
            Some('\n') => { *index += 2; continue; }
            Some('\r') if raw_chars.get(*index + 2).copied() == Some('\n') => { *index += 3; continue; }
            _ => {
                simulated.push(c);
                *index += 1;
                continue;
            }
        }
    }
    _ => 分支推入 \ 后只做了 *index += 1 就 continue,下一轮循环直接把被转义的字符当成未转义字符处理:
    • 若反引号内含嵌套反引号 \``,下一轮看到 `` `` 会立即 break 提前退出反引号循环;
    • 随后嵌套反引号内的 )(如 \\) 在外层 $() 循环中 \\ 被配对消费后留下的裸 ))会把 $() 的 depth 减到 0 提前闭合 $(),尾部文本落入外层模拟循环,导致 simulated != cooked 触发 bail。
  • 复现(WSL GNU Bash 5.3.0 对拍):
    printf "<%s>\n" "a b"$(echo `echo \`echo \\)\`` \$y)
    • WSL GNU Bash 5.3.0:
      <a b)>
      <$y>
      
    • 本 PR (80aea329):\`` 提前闭合反引号、\)中的)提前闭合$(),尾部 ``\ \$y)`` 按外层词转成 carrier 导致simulated != cooked` bail,引号内空格被拆开:
      <a>
      <b)>
      <$y>
      
  • 建议修法:在 copy_comsub_body_verbatim_for_ifs_marking 的反引号 \ _ => 分支中成对推入 \ 与被转义字符并前进 2 个字符(对齐 copy_backtick_body_preserving_syntax)。

3. [Major] Face 1(反引号内 \<newline> / \<CRLF> 消除)遗漏了外层未引用反引号分支

  • 位置:src/executor/command_prepare.rs:4608-4627(if ch == '' && !in_single && !in_double`)
  • 原因:
    权威 lexer 路径在外层未引用反引号(src/lexer/quotes.rs:355)调用的是 copy_backtick_body_cursor(&mut out, &rest[1..], false),同样会消除 \<newline> 与 \<CRLF>(quotes.rs:686-688, 704-706)。
    本 PR 在 in_double 反引号分支(command_prepare.rs:4649-4658)和 $() 内部反引号分支(command_prepare.rs:189-199)加上了 \<newline> / \<CRLF> 消除,但**外层未引用反引号分支 if ch == '' && !in_single && !in_double(command_prepare.rs:4608-4627)未做修改**,仍然原样拷贝 ` 和 \n。
    (注:tests/issue880_comsub_case_ifs_mark_regressions.rs:59-64 的 "a b"\echo x\\ny`之所以通过,是因为GroupScanFeeder::push_main_line在src/lexer/mod.rs:1122已经把行尾续行\\n剥掉,进入finish_word_token的raw里已经没有\\n;一旦 raw携带未消除的<newline>进入mark_quoted_ifs_from_raw,外层反引号分支仍会与 cooked` 失配并 bail。)
  • 建议修法:将 command_prepare.rs:4608-4673 的两个 ch == '' && !in_single分支合并为一个(以in_double作为dq标志,完全对齐lexer/quotes.rs:676的copy_backtick_body_cursor(out, rest, dq)),既消除重复代码,又保证 dq=false与dq=true都执行<newline>/<CRLF>` 消除。

@caomengxuan666

Copy link
Copy Markdown
Member

独立复核(80aea329)— 确认第一轮评审的 2 个 Blocker,暂缓合并

按第一轮评审的三个点做了独立三路对拍(master a83a09d3 二进制 / 本分支二进制 / WSL GNU bash 5.3.0,/usr/local/bin/bash 单脚本批量电池:33 个 LF 形 + 3 个内嵌 CR 形),结论:Blocker 1、Blocker 2 均实证复现,仍按 REQUEST CHANGES 退回;Major 3 属代码不一致(当前输入层剥续行,电池内无可达分歧)。方向本身(按 lexer/quotes.rs 权威补全模拟器 walk、复用 update_command_substitution_case_depth)没有问题,夹带检查干净(5 文件全对题),请修完以下几点再推。

1. [Blocker] Face 2 命令词路径仍 100% bail —— 模拟器推 DATA_DQUOTE 而 cooked 是裸 "

复现(命令词:含未引用展开兄弟才会进 mark_quoted_ifs_from_raw):

w="p q"
printf '<%s>\n' "a b`echo \"x\"`"$w
侧 输出
WSL GNU 5.3.0 <a bxp> + <q>
master <a> + <bxp> + <q>(错拆)
本分支 <a> + <bxp> + <q>(仍 bail 错拆)

机理与第一轮评审一致:cook 侧两条路径——copy_backtick_body_cursor(dq=true)(quotes.rs:572/993)与本 PR 新增的 copy_backtick_body_preserving_syntax_dq——都把 \" 折成裸 "(0x22),但 command_prepare.rs:4645 的模拟器 '"' => 臂推的是 DATA_DQUOTE(markers.rs:102,\u{18})。\u{18} != '"' → simulated != cooked → 必然 bail。修法:改推 '"'。

现有 dquote_wrapped_backtick_escaped_quote_folds 测的是纯赋值词(走 remove_shell_quotes_outside_backticks,不进 mark_quoted_ifs_from_raw),所以没抓到。请补上面这个命令词形的回归测试(assert_script 直接可用)。

2. [Blocker] copy_comsub_body_verbatim_for_ifs_marking 反引号体 _ => 臂单步进,\` 提前闭合

复现:

printf '<%s>\n' "a b"$(echo `echo \`echo \\)\`` \$y)
侧 输出
WSL GNU 5.3.0 <a b)> + <$y>
master <a> + <b)> + <$y>(错拆)
本分支 <a> + <b)> + <$y>(仍 bail 错拆)

机理:反引号体内 _ => 臂只推 \ 并 index += 1,被转义的 ` 下一轮被当闭合符 → 反引号提前闭合 → ) 提前闭合 $() → 尾部落入外层模拟 → bail。同函数的顶层 \\ 臂(成对推入并 +2)与 copy_dquote_span_verbatim_for_ifs_marking 的 \\ 臂都是成对处理,唯独这里不一致。修法:成对推入 \ + 被转义字符并 index += 2,对齐 copy_backtick_body_preserving_syntax(_dq)。

3. [Major/一致性问题] 外层未引用反引号臂仍缺 \<newline> / \<CRLF> 消除

command_prepare.rs:4608-4627(!in_single && !in_double)原样拷贝 \+换行。目前电池内 p01/p10 三方一致(GroupScanFeeder::push_main_line 已在输入层剥主行续行,raw 到不了带 \<LF> 的形态),属不可达的潜伏不一致。建议按第一轮评审方案:把两个 ch == ''分支合并为带dq 标志的一个,dq=false` 同样做消除,顺带消重。

4. 对抗矩阵新发现(存量、非本 PR 引入,属 Face 2 同族,请顺手覆盖或另开票跟踪)

同族形里还有两个形在 master 与本分支上同样错(不 bail,但 \" 不折叠、引号漏进输出):

printf '<%s>\n' $"a b`echo \"x\"`"$w      # GNU: <a bxp>   master/分支: <a b"x"p>
printf '<%s>\n' "a b`echo \"x\"`"$(echo p q)   # GNU: <a bxp>   master/分支: <a b"x"p>

这两个形不走赋值路径也不 bail(simulated 恰与 cooked 相等),说明命令词 cook 路径上 \" 折叠并未真正生效。修 Blocker 1 时建议一并核对这条路径,避免修完模拟器后 cooked 仍然错。

另注记一条存量(不属于本票,仅供知悉):内嵌字面 CR 的 \+CR+LF 形(CRLF 脚本场景),master 与分支同为 <a bxyp>,GNU 5.3.0 为 <a bx·p>(CR 保留为数据)——输入层既有分歧,本 PR 新加的 \r\n 消除臂实际不可达。

5. 对拍与回归对照(无新红)

  • 电池 33 LF 形:分支 30/33 逐字节对齐 GNU(唯二 bail 即上面 1/2,外加第 4 点 2 形中 p33 一处,p31 与 p03/p08 有重叠行)。CR 3 形 master==分支(存量)。相对 master:修复确认 Face 3 全 4 形 + \$/\\("a becho $x \"$w)+ case 体内 $'..' 共 7 形,零新增分歧。
  • tests/issue880_comsub_case_ifs_mark_regressions 6/6、issue868 7/7、issue876 7/7 绿;--test regression 77/78,唯一失败 upstream_suite_slice_snapshots 在今晨 master 基线(base843_fails.txt)内,非本 PR 造成。
  • CI 7 项绿已确认;diff 5 文件(command_prepare/lexer mod/quotes/skip/新测试)全部对题,无夹带。

结论

修 Blocker 1(模拟器推裸 ")、Blocker 2(反引号体 _ 臂成对步进),补两条命令词形回归测试(含未引用展开兄弟),建议顺带处理第 3、4 点。推新 commit 后我再跑同一电池复核。

@caomengxuan666

Copy link
Copy Markdown
Member

Rework PR opened: #936 — implements all findings from the review above (comment 6290178882), built directly on this branch's head 80aea329 so the original authorship is preserved.

maintainerCanModify is false on this PR so the fixes could not be pushed here directly. Per the findings:

  • Blocker 1: simulator's in_double backtick arm now pushes a bare " (matches copy_backtick_body_cursor(dq=true) cooked bytes) — the command-word reproducer prints <a bxp> + <q>.
  • Blocker 2: copy_comsub_body_verbatim_for_ifs_marking backtick-body _ arm now consumes \+escaped as a pair — the nested \` reproducer prints <a b)> + <$y>.
  • Major 3: the two outer backtick arms merged into one dq-flagged arm; dq=false now also elides \<newline>/\<CRLF>.
  • Point 4: $"..." flipped to preserve_backticks=true on the command-word walk (the false was left over from d59517c; the assignment walk's $" arm already passes true) — both point-4 forms now byte-match GNU 5.3.0. This also keeps the Blocker-1 fix from regressing the $" shape.
  • New regression tests added for the command-word forms with unquoted expansion siblings.

@a-rookie-of-C-language remains the author of record for the #880 fix; whichever of the two PRs merges first wins.

@caomengxuan666

Copy link
Copy Markdown
Member

Review findings implemented in #936 (merged to master as b934174).

@caomengxuan666

Copy link
Copy Markdown
Member

The two blockers this PR targets have landed via #936 (squash b934174 — p03 DATA_DQUOTE/dereference mis-split and p08 backtick-body single-step early close, both verified byte-identical to GNU). Thank you @a-rookie-of-C-language — closing as superseded; branch remains for reference.

@caomengxuan666
caomengxuan666 deleted the arookieofc/fix-880-mark-quoted-ifs-walk branch October 21, 2026 08:07
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.

mark_quoted_ifs_from_raw 的三个 bail 盲点(backtick elision、dquote 内 \" 折叠、$() case 早闭)——补全或文档化

2 participants