Skip to content

fix(parser): arithmetic-for sections evaluate the verbatim (( )) capture, carrier bytes never reach evalerror echoes (#548) - #584

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/548-arith-echo-control-char
Oct 13, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/548-arith-echo-control-char

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

问题

for (( i="1;2" ; i<3 ; i++ )); do :; done 的算术错误回显泄漏内部标记字节 0x1c(issue #548,P3):rubash 报 i=\x1c1;2,GNU 逐字回显 i=1;2 。

字节级取证(WSL GNU bash 5.3.0 stdin oracle,od -c)

修复前(origin/master 7f18b44,逐字节):

<argv0>: line 1: ((: i= 1c 1;2: arithmetic syntax error: operand expected (error token is " 1c 1;2")

GNU bash 5.3.0:

bash: line 1: ((: i=1;2 : arithmetic syntax error: invalid arithmetic operator (error token is ";2 ")

修复后与 GNU 逐字节一致(含 i=1;2 尙留的尾随空格与 error token ";2 "),0x1c 不再出现。

根因(file:line)

src/parser/arithmetic_for.rs 把三个 section 的求值文本建为 token.value 归一化 join。带引号的 word 在 token value 里以载体标记协议传输:"1;2" 的值是 1c 1;2(0x1c = 引号胶水位标记,src/executor/markers.rs)。该标记字节一路进入算术求值器输入,既毒化求值也进入 evalerror 回显渲染(ArithEvalError::render,src/executor/arithmetic/parser.rs)。

语义契约锚定(vendored bash,仅引用不搬运)

  • third_party/bash/make_cmd.c:254-261 make_arith_for_command:section 文本 = 原文逐字(段首空白跳过、引号保留)。
  • third_party/bash/execute_cmd.c:3171 eval_arith_for_expr:求值前先做引号剥离,求值器永远看不到引号字节。
  • rubash 侧:section 求值输入改为逐字 capture(raw_parts,段首 trim,注册表码点在诊断边界剥离);求值器内部的原地引号删除(bare (( )) 路径已与 GNU 字节一致)随后自然复现 GNU 组合。

附带修复(同根因)

for (( i="3" ; i>0 ; i-- )):修复前整个循环因 i=\x1c3: operand expected 中止;修复后与 GNU 一致输出 hi x3。

验证

  • cargo check + RUSTFLAGS="-D warnings" cargo check 通过;cargo fmt --check 干净。
  • 新增 tests/issue548_arith_for_carrier_echo_regressions.rs:4 测试绿(0x1c/C0 载体字节不出现 + GNU 逐字 body 断言 + 成功路径 rc/stdout)。
  • 逐字节对比 GNU:issue 复现、for (( i="a;b" ; 0 ; 1 ))、for (( 7++ ; ... ))、裸 for (( i="1;2" )) 解析错形态均 md5(identical)。
  • 全量 RUST_TEST_THREADS=1 cargo test --no-fail-fast:失败集与基线(origin/master 独立工作树)双向 comm — fix-only 为空;base-only 仅 upstream_suite_slice_snapshots(既有本地环境失败,与本改动无关,基线工作树单独复现)。
  • 两个既有 parser 断言按逐字 capture 契约更新(update 段尾随空格 i++ ,make_cmd.c 保留 ; 前全部文本)。

Closes #548

…ure, carrier bytes never reach evalerror echoes (#548)

GNU contract: make_cmd.c:254-261 splits the ARITH_FOR_EXPRS text into
three sections by scanning with skip_to_delim, keeping each section's
text verbatim (leading whitespace skipped, quotes intact);
execute_cmd.c:3171 eval_arith_for_expr strips quotes before evalexp,
so evalerror echoes the dequoted section and the evaluator never sees
a carrier byte.

rubash's parser built each section as a token-value join instead:
quoted sections travel through token values as carrier-marked text,
and the 0x1c quoted-glue carrier reaching the arithmetic evaluator
(a) failed the evaluation - for (( i="3" ; ... )) aborted with
'operand expected' where GNU bash 5.3.0 runs three iterations - and
(b) leaked the raw carrier into the evalerror echo (i=\x1c1;2 where
GNU echoes 'i=1;2 ').

Feed the evaluator the whitespace-carrying capture (leading blanks
trimmed, registry codepoints dropped at the diagnostic boundary); the
evaluator's in-place quote removal then reproduces the GNU
composition byte for byte:

  for (( i="1;2" ; i<3 ; i++ )); do :; done
    GNU 5.3.0: bash: line 1: ((: i=1;2 : arithmetic syntax error: invalid arithmetic operator (error token is ";2 ")
    before:    ((: i=\x1c1;2: arithmetic syntax error: operand expected (error token is "\x1c1;2")
    after:     byte-identical to GNU (section trailing blank kept in echo and token)

Byte-verified vs WSL GNU bash 5.3.0 (stdin oracle, od -c): the issue
repro, for (( i="a;b" ; 0 ; 1 )), for (( 7++ ; ... )), and the bare
for (( i="1;2" )) parse-error form are byte-identical after the fix;
for (( i="3" ; i>0 ; i-- )) prints hi three times like GNU.

Parser expectations for the update section now record the verbatim
capture's trailing blank before )) (make_cmd.c keeps everything up to
the section-terminating ';'). New
tests/issue548_arith_for_carrier_echo_regressions.rs asserts
carrier-byte absence and the GNU-verbatim diagnostic bodies.
@caomengxuan666
caomengxuan666 merged commit b5e1a2f into master Oct 13, 2026
8 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/548-arith-echo-control-char branch October 14, 2026 04:15
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.

[cosmetic] for (( i="1;2" )) 算术错误回显泄漏原始控制字符 0x1c:i=\x1c1;2(GNU 逐字回显 i="1;2)(P3)

1 participant