Skip to content

[bug] Command substitution captures nothing when the substituted program is the rbenv dispatcher script (output leaks to real stdout): rbenv init eval chain defines no function #193

Closed

Description

@caomengxuan666

最小复现(wt/o3-platform 快照 c9bd0af+#175 修复,同样存在于 8f89767 产物)

# cap2.sh — L 是 rbenv 1.3.2 的 libexec/rbenv(bash 脚本,exec rbenv---version 收尾)
L=/d/repo/rubash/target-ecosys5/tools/rbenv/libexec/rbenv
x="$($L --version)"
echo "v=[$x] len=${#x}"

期望(WSL GNU 5.3.0):stdout 只有 v=[rbenv 1.3.2] len=11。
实际(rubash):rbenv 1.3.2 直接打到真实 stdout,v=[] len=0 —— 捕获为空。

已排除的形态(这些都能正常捕获)

  • 最小 shebang 脚本 ``:OK
  • 捕获脚本内部再嵌套 $(echo):OK
  • 脚本内 export PATH=...:"$PATH" 后 exec <bare-name>(PATH 找到):OK
  • "$L" init - bash > file 文件重定向:OK(rc=0,内容正确)

实伤

  • p02-rbenv C2 探针(target-ecosys5/p02-rbenv.sh)eval "$("$RB" init - bash)":stage1 文本泄漏到 stdout,rbenv() 函数未定义,后续全部 command not found(rc=127),与 8f89767 产物 target-ecosys5/results/p02-rbenv/rub.out 形态一致 —— 阻塞 rubash#184 的探针验收(${PATH%%:*} 归一本身已修)。
  • 同一探针在 ruby-build --version 步骤处 shell 直接终止(out 截断、rc=1,无 timeout 124);单独运行 ruby-build --help/--version(两种路径形态)rc=0 不再挂死 —— rubash#185 的挂死在孤立复现下已被基线修复消化,剩余失败在此泄漏家族内。

产物

  • 泄漏复现:target/probe/cap2.sh 及输出(wt-o3-platform)
  • p02 全量输出:D:/repo/rubash-wt-o3-platform/target/probe/p02rub.{out,err}
  • 语料:D:/repo/rubash/target-ecosys5/tools/rbenv(rbenv 1.3.2)、tools/ruby-build

初步定位方向

libexec/rbenv 在 exec 前经过 canonicalize()(type -P readlink + $($readlink -f ...))、shopt nullglob、${BASH_SOURCE:-$0} 等路径后 exec rbenv---version(PATH 内裸名)。exec-of-script 链的 stdout 捕获在某一步绑回了控制台句柄;需 LLDB 看 external spawn/上游脚本执行路径的 fd 继承。

Activity

  1. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    更正(上文两处内联替换在提交时被本地 shell 吃掉):

    • 已排除形态之二应为:捕获脚本内部再嵌套命令替换 $(echo nested) —— OK。
    • 初步定位中的 canonicalize 内联应为:readlink 路径探测 resolved_path="$("$readlink" -f "$1")"。
  2. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

    Fixed on wt/p3-comsub (b0728d30, base 26728da).

    Minimal reproducer (narrower than the rbenv chain — the trigger is a VARIABLE first word in the comsub, which routes the body through the full-parser path instead of the external spawn fast path):

    # s2.sh:  #!/bin/sh\n echo pre\n exec sort --version
    L=./s2.sh
    x="$($L)"

    rubash before: pre captured, sort --version output LEAKED to the real stdout, x=[pre]. Literal $(./s2.sh) captured fine — only the variable-first-word form leaks.

    GNU anchor: builtins/exec.def exec_builtin → shell_execve (execute_cmd.c:6126-6159): the exec'd program inherits fd 1, which inside a comsub (subst.c:7143 command_substitute) is the capture pipe.

    Root cause: rubash runs such scripts in-process on the full-parser comsub path, where fd 1's Stdout endpoint resolves to the in-memory stdout_capture — no inheritable OS handle. execute_exec's default arm translated that endpoint to Stdio::inherit() = raw process stdout. Fix: when fd 1's endpoint is Stdout and a capture is active, run the exec'd child piped via execute_with_io and forward through the capture-aware GlobalStdout (same shape as run_external_command_substitution); exec cmd 2>&1 in the substitution joins the capture, plain fd 2 stays the terminal. Non-capture arms unchanged.

    Verification (WSL GNU Bash 5.3.0): x="$($L --version)" → v=[rbenv 1.3.2] len=11 (the expected output in this issue); rbenv init - bash stage-1 captured (p02 shape); exit statuses for exec false (1) / exec missing (127, diagnostic captured under 2>&1) identical to GNU. Suites comsub/comsub-eof/comsub2/dstack/dstack2/errors ledger unchanged (25/0/66/0/0/2). The one d-len byte difference in the sort probe is WinuxCmd sort 8.32 vs WSL coreutils 9.4 version text — environment artifact, not a divergence.

  3. caomengxuan666 commented on Sep 27, 2026

    @caomengxuan666
    MemberAuthor

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

  4. added
    bug
    family:S5
    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

    bugfamily:S5

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions