Skip to content

fix(fc): align sole-current-entry index clamp and fc -s rejection with GNU fc.def (Fixes #4247) - #4257

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/4247-fc-sole-current-entry-index
Nov 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/4247-fc-sole-current-entry-index

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Align fc sole-current-entry index resolution with GNU Bash 5.3.20 (Fixes #4247):

GNU C Specification

  • third_party/bash/builtins/fc.def:91: HIST_INVALID is INT_MIN (not -1).
  • third_party/bash/builtins/fc.def:334-350 (fc_builtin()): computes last_hist = i - rh - hist_last_line_added, backs up over the null tail slot, and clamps its own last_hist < 0 to 0 ("per POSIX").
  • third_party/bash/builtins/fc.def:593-600 (fc_gethnum()): recomputes the raw last_hist and returns the ordinary signed integer -1 (NOT HIST_INVALID / HIST_ERANGE) when last_hist < 0.
  • third_party/bash/builtins/fc.def:379-399 (fc_builtin()): sentinel checks (HIST_INVALID, HIST_ERANGE, HIST_NOTFOUND) run first, then ordinary negative indices (-1) are clamped to 0.
  • third_party/bash/builtins/fc.def:679-684 (fc_gethist()): accepts only non-negative indices (i >= 0), so the execute form (fc -s, including bare fc -s) reports fc: no command found instead of recursively re-executing the in-flight fc -s entry.

Changes

  • src/builtins/fc.rs:
    • Changed fc_gethnum() to return Result<i64, SpecErr>, returning Ok(-1) when raw_last_hist < 0 (fc.def:599-600).
    • Split raw_last_hist (passed to fc_gethnum and bare fc -s) from POSIX-clamped last_hist = raw_last_hist.max(0) (fc.def:349-350).
    • In execute (fc -s): reject negative resolution (both -1 and sentinels) with fc: no command found (fc.def:679-684), fixing both explicit specs and the bare fc -s self-recursion hang.
    • In list/edit forms: clamp ordinary negative histbeg/histend to 0 after sentinel checks (fc.def:395-399).
    • Renamed internal SpecErr::Erange to SpecErr::Invalid to reflect HIST_INVALID.
  • tests/issues_4200_4299.rs + tests/issues_4200_4299/issue4247_fc_sole_current_entry.rs:

What this PR deletes

Verification

  • WSL GNU Bash 5.3.20 (/usr/local/bin/bash) matrix: 95/95 noninteractive cases byte-exact (set -o history sole-entry & prior-entry, set +o history 0/1/2 entries, HISTIGNORE='fc*', command substitution, edge specs).
  • WSL Linux:
    • RUSTFLAGS="-D warnings" cargo check --lib --locked: clean (0 warnings).
    • cargo test --lib builtins::fc --locked: 34/34 passed.
    • cargo test --test issues_4200_4299 --locked: 7/7 passed.
  • Windows:
    • cargo test --lib builtins::fc --locked: 34/34 passed.
    • cargo test --test issues_4200_4299 --locked: 7/7 passed.
    • python scripts/check-rustfmt.py: ok (226 targets).
    • python scripts/check-marker-literals.py: OK.
    • python scripts/check-api-ledger.py: OK (298 symbols).

Fixes #4247

@caomengxuan666

Copy link
Copy Markdown
Member Author

队长只读复核:达合入门,可进运行时复验(不合并不代表已验证,运行时仍须真 oracle 探针 + fail-first)。

GNU 锚点逐项核对(vendored GNU bash 5.3.20 fc.def,third_party/bash 子模块内容)

  • fc.def:91 = ✓;:93 = ✓
  • fc.def:334-350:last_hist = i - rh - hist_last_line_added,空尾槽回退,自身 last_hist < 0 钳为 0("per POSIX")✓ —— PR 的 raw_last_hist / last_hist = raw.max(0) 拆分与之一致
  • fc.def:593-600:fc_gethnum 在解析 spec 之前遇 last_hist < 0 返回普通整数 -1(非 HIST_INVALID)✓ —— PR 的 fc_gethnum 顶端 Ok(-1) 与之一致
  • fc.def:379-399:哨兵检查(INVALID/ERANGE → Erange 文案,NOTFOUND → no command found)在先,普通负索引随后钳为 0 ✓ —— PR 的 sentinel match + clamp 顺序与之一致(list/edit 两路同形,fc.def:423-445 亦同)
  • fc.def:679-684:fc_gethist 只接受非负索引,负值 → NULL → fc: no command found ✓ —— PR 的 execute 分支(含裸 fc -s 的 None => Ok(raw_last_hist))与之一致,消掉了旧 last_hist.max(0) 旁路导致的自递归
  • fc.def:650-655 / 662-665:数值越界 HN_FIRST ? 0 : i、字符串最晚匹配优先、无匹配 HIST_NOTFOUND ✓ —— 移植分支逐一对得上

改动面

仅 src/builtins/fc.rs(+120/−61)+ 新测试桶 tests/issues_4200_4299(.rs)(+194),无顺带改动。治理:PR 写明删除项(错的 Erange 早返、SpecErr::Erange 变体、裸 fc -s 的 max(0) 旁路、#4233 临时 Erange 钉),无第二走法、无新增 dead_code。

新钉覆盖 #4247 全部面

  • 单元钉:listing 7 形(-l/0/1 2/-1/99/-99/nosuchcmd,全 rc 0 列出当条)、execute 4 形(-s/0/1/nosuchcmd,全 no command found)、prior-entry 对照(fc -l 0 仍选前条)
  • 集成钉:sole-entry listing、execute、prior-entry 对照、history-off 空表/ pushed 1/2 条、comsub 两形;mod common; + #[path] 写法与既有桶(issues_3900_4199)一致,wait_bounded 在 tests/common/mod.rs 有定义

CI 名字集差分(run 46097268210)

运行时复验待办(队长另派 devin-mac)

  1. 作者称的 WSL 95/95 矩阵不作 oracle 证据(WSL bash 非 tarball oracle,fix(redir): unify RX_UNDOABLE move verdict across respawn payload faces and async compound roots (Fixes #4046) #4251 已有此口径),须在云端 bash-5.3.20 oracle 上重跑全部 95 形
  2. edit 带 spec 形(fc 0 / fc -e)在 sole-current-entry 下未钉,复验时以 oracle 探针补上
  3. 确认新集成测试目标(issues_4200_4299)在 CI/本地实际执行、非静默跳过;新钉在 base 上 fail-first

@caomengxuan666

Copy link
Copy Markdown
Member Author

订正上一条评论的一行(shell 反引号吞字):fc.def:91 行应为 HIST_INVALID = INT_MIN;fc.def:93 行应为 HIST_NOTFOUND = INT_MIN+2。其余锚点行无损,裁决不变:达合入门,可进运行时复验。

@caomengxuan666
caomengxuan666 merged commit 66eeaba into master Nov 28, 2026
11 of 13 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/4247-fc-sole-current-entry-index branch November 28, 2026 03:17
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.

[gnu-baseline] fc: first-command fc -l 0 reports Erange where GNU lists (index model lands last_hist=-1 on sole current entry)

1 participant