Skip to content

WIP: fix(executor): anchor function-call redirect operands at the session cwd - #2247

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/2240-func-redir-cwd
Oct 30, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/2240-func-redir-cwd

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Refs #2240

问题

函数调用的输入重定向相对路径按进程启动 cwd 解析而非当前逻辑 cwd:cd 后 g <f.txt 对当前目录里的文件报 ENOENT(t1300 α 族 4 条)。#2211 的姊妹病、不同代码路径——#2211 锚的是 comsub 快捷路径,本条在函数调用重定向一线。

GNU 依据:GNU 只有一个 cwd——cd 经 builtins/cd.def 的 change_to_directory 直接 chdir 进程;execute_function(execute_cmd.c:5181)以 do_redirections 应用调用的重定向,redir.c 的 redir_open 对展开后的词做普通 open,相对操作数即按 shell 当前目录解析。rubash 的 G-02 模型下进程 cwd 永不移动,一切操作数解析必须经 session_path 锚到 ShellState.cwd。

审计与修复(三处同病灶,统一走既有解析梯)

全仓盘点重定向操作数的裸 open 点后,确认三处未锚定(其余各面——简单命令 open_input_redirect、编号 fd open_compound_numbered_input_redirects、复合 fd0 compound_fd0_stream_file、函数输出侧 create_redirect_output_prepared、动态 fd 与 exec 的 set_fd_readwrite_file——均已锚定):

  1. function_call_stdin(function_calls.rs)——函数调用 < file 的实际绑定(含定义时重定向在每次调用时的应用):cd 后 ENOENT,同名文件在启动目录存在时还会读错内容。
  2. stdin_string_for_command(shell_options.rs)——文本通道对 <> 的预创建与 slurp open 未锚定:函数 <> 读到启动目录的文件/空文件。
  3. reject_invalid_redirects 的 ReadWrite 探针(redirection.rs)——全命令形式共享的校验门:<> 探针的 open 带 O_CREAT([bug] '<> file' does not create the missing file (GNU opens O_RDWR|O_CREAT; rubash fails ENOENT) #264),未锚定时在启动目录留下一个空的杂散文件,且对着错误文件下校验结论(简单命令 cat <>new 在两个目录各建一份即此)。

三处统一改为与其余输入面相同的解析梯(shell_path_to_windows → windows_operand_file_path → session_path),并保留 #2211 的空操作数规则:空词永不 join 到 cwd(否则会把会话目录本身制造出来)。诊断继续按展开后的操作数原样命名(path_error 吃 target 本身),不暴露锚定后的绝对路径。

验证

  • 新钉 tests/issue2240_function_input_redirect_cwd_regressions.rs(14 条):基线(master 1d5d62d)9 红 5 绿(5 绿均为对照:启动 cwd、绝对路径、编号 fd、缺失文件诊断、输出侧锚定),修复后 14/14 绿。
  • 探针电池 probeV/W/X/X7/Y/E/E2(分诊种子 + 本簇新增的 <>、定义时重定向按调用时 cwd、..、ENOEXEC 子脚本形状)修复后与 GNU bash 5.3.20 逐字节一致,含目录树(<> 创建落点)与 stderr。
  • 相邻目标无回退:issue2211(14)、issue2219(9)、issue2220(9)、issue2221(5)、issue1391(3)、heredoc_regressions(3)全绿。

范围外(已在簇 REPORT 记录、供主会话立案判断)

函数调用 <> 的写面(函数体向 fd 0 写)在基线上即与 cwd 无关地坏(echo x >&0 报 Bad file descriptor)——属 fd-0 写端点建模的独立缺口,本 PR 不动。

caomengxuan666 and others added 2 commits October 30, 2026 16:22
GNU keeps exactly one cwd: cd moves the process's
(builtins/cd.def change_to_directory -> chdir), and execute_function
applies the call's redirections with do_redirections
(execute_cmd.c:5181), whose redir_open (redir.c) opens the expanded
word relative to that cwd. Under G-02 the process cwd never moves,
so every operand resolution must anchor at ShellState.cwd via
session_path. Three faces of the function-call / shared redirect
machinery opened the bare mapped path instead, resolving against
the startup directory after any cd:

- function_call_stdin (function_calls.rs): a function call's
  `< file` (and a definition redirect's `<`, applied per call)
  missed files in the current directory — ENOENT, or the startup
  tree's same-named file read instead (t1300 alpha family).
- stdin_string_for_command (shell_options.rs): the text channel's
  `<>` create and slurp open did the same, so a function call's
  `<>` staged the startup tree's file.
- reject_invalid_redirects' ReadWrite probe (redirection.rs): the
  shared validation gate's `<>` probe creates its target
  (rubash#264) — unanchored, it planted a stray empty file in the
  startup directory for every command form and passed its verdict
  against the wrong file.

All three now resolve through the ladder the other input faces
already use — shell_path_to_windows, windows_operand_file_path,
session_path — with the rubash#2211 empty-operand rule preserved
(an empty word is never joined onto the cwd). Diagnostics keep
naming the operand as spelled (path_error on the expanded target).

Regression pins: tests/issue2240_function_input_redirect_cwd_regressions.rs
(9 defect pins red on master 1d5d62d, 5 controls green; all 14
green with this change; probe battery V/W/X/X7/Y/E byte-identical
to GNU bash 5.3.20).

Refs #2240
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 30, 2026 16:33
@caomengxuan666
caomengxuan666 merged commit 1077ff8 into master Oct 30, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/2240-func-redir-cwd branch November 2, 2026 01:08
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.

1 participant