Skip to content

fix(builtins,executor): #704 printf numeric validation (R6.1), command -v no-operand + substitution status (R6.2 residue) - #869

Merged
caomengxuan666 merged 2 commits into
masterfrom
fix/704
Oct 19, 2026
Merged

caomengxuan666 merged 2 commits into
masterfrom
fix/704

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Summary

Two facets of #704. R6.2's multi-operand rc rule landed earlier (5da6421); this closes its two residue holes and the whole R6.1 printf numeric-validation facet.

R6.1 — printf numeric validation

GNU getintmax/getuintmax/getdouble (printf.def:1427/1450/1472) hand operands to strtoimax/strtoumax/strtod: only leading C-locale white space is skipped, the longest valid subject sequence is converted, and that value is returned even when chk_converror (printf.def:1388-1395) rejects the trailing bytes and sets conversion_error (exit 1, processing continues — POSIX contract quoted in the source). rubash previously trim()-ed both sides ('5 ' silently passed for %d) and Rust's f64::from_str dropped suffixed operands to 0.0 for %f. src/builtins/printf/number.rs now scans the actual subject sequence:

  • trailing-white-space operands: value kept + printf: 5 : invalid number + rc 1 (all of %d %i %u %x %X %o %f %e %g %a)
  • chk_converror branch order: *ep != 0 wins over ERANGE (99999999999999999999x reports "invalid number", not erange)
  • sh_invalidnum classification (builtins/common.c:214-225) mirrored on the full original operand: 0Xzz → "invalid number" (only lowercase x is hex), 01z and suffixed 055␠/08-forms → octal, 0x-forms → hex, 0␠ → number
  • glibc 2.38+ base-0 0b/0B binary literals (0b101 → 5; 0b2 → 0 + "invalid number") — oracle-verified
  • asciicode (printf.def:1519-1544) runs before any numeric parse with no chk_converror: lone quote ' → NUL (0, silent), 'a␠ → 97
  • getdouble subject scan adds hexadecimal floats (0x1, 0x1p3, 0x.8p1) and inf/infinity/nan(n-char-seq); non-finite results render per conversion case (inf/nan vs INF/NAN for %F/%E/%G/%A), keep the sign bit (-nan), and still take %+ (+inf) — the old engine could never deliver a nan operand to the renderer at all

R6.2 residue — command -v

builtins/command.def:95-96: if (list == 0) return EXECUTION_SUCCESS; — the no-operand early return precedes the describe loop. The executor's command -v/-V path (type_builtin.rs) fell through to the any_found accumulator and returned 1 for bare command -v. The substitution entry point (command_substitution_values.rs) still used an all-found accumulator — $(command -v echo nosuch) wrongly exited 1 — and dropped the -V "not found" diagnostic; now any-found + stderr routing (oracle: stdout captures the description, stderr gets command: nosuch: not found, rc 0).

type is the all-found builtin and keeps its rule (control-tested).

Verification

  • Oracle: WSL GNU bash 5.3.0 (/usr/local/bin/bash), script-file byte compare, 110-case matrix (all five R6.1 ticket reproforms + white-space/suffix/radix-prefix/overflow/char-constant/float-subject/command-operand boundaries): 107/110 byte-identical. The 3 remaining are pre-existing engine divergences outside this facet, reproducible on master unchanged: %f 1e999 (oracle strtofltmax long-double expansion vs f64 inf) and %a mantissa style (0xap-1 vs 0x1.4p+2; plain printf '%a\n' 5 diverges on master).
  • Regressions: src/builtins/printf_tests.rs mod numeric_validation_704 (14 cases); tests/regression.rs issue_704 gains 704.2b (no-operand success, direct + substituted) and 704.2c (substitution any-found + -V stderr). printf family 52/52, issue_704 3/3, full tests/regression.rs 78/78 — no printf or command -v family regression.
  • Full suite: cargo test --no-fail-fast -- --test-threads=1 on this branch vs pristine master (45f537e) in a separate worktree: failure sets identical (34 machine-state failures both sides, 0 new, 0 flapped). cargo fmt --check clean; clippy: no warnings in any touched code region (tree-wide pre-existing warnings unchanged, count 557 → 556 after merging master's 注释/文档漂移清扫:#838 删除的断言引用、subst.c 坐标漂移、clippy cast(#841/#838/#426 CR nits 打包) #849 hygiene sweep).

Closes #704 R6.1; R6.2 fully closed (multi-operand 5da6421 + residue here). R6.3/R6.4/R6.5 remain open in the issue thread.

…d -v no-operand + substitution status (R6.2 residue)

R6.1 printf numeric validation. GNU getintmax/getuintmax/getdouble
(printf.def:1427/1450/1472) hand operands to strtoimax/strtoumax/strtod,
which skip only LEADING C-locale white space, convert the longest valid
subject, and RETURN that value even when chk_converror
(printf.def:1388-1395) rejects the trailing bytes and sets
conversion_error (exit 1, processing continues). rubash's parse_* trimmed
BOTH sides (so '5 ' passed silently for %d/%i/%u/%x/%X/%o) and Rust's
f64 parse dropped suffixed operands to 0.0 for %f. number.rs now scans
the strtod/strtoimax subject sequence:

- trailing-white-space operands report sh_invalidnum
  (builtins/common.c:214-225) while keeping the converted value:
  'printf %d "5 "' -> 5 + "5 : invalid number", rc 1
- chk_converror order: the *ep != 0 branch wins over ERANGE, so
  99999999999999999999x reports "invalid number", not erange
- sh_invalidnum classification mirrored on the full original operand:
  0Xzz -> "invalid number" (lowercase x only is hex), 01z and
  suffixed 055-/08-forms -> octal, 0x-forms -> hex
- base-0 0b/0B binary literals convert like glibc 2.38+ strto*
  (oracle-verified: 0b101 -> 5; 0b2 -> 0 + "invalid number")
- asciicode (printf.def:1519-1544) runs BEFORE any numeric parse and
  does no chk_converror: a lone quote is the NUL at arg[1] (0, silent),
  "'a " stays 97
- getdouble's subject scan adds hexadecimal floats (0x1, 0x1p3,
  0x.8p1) and inf/infinity/nan(n-char-sequence); non-finite results
  render per conversion case (inf/nan vs INF/NAN for %F/%E/%G/%A),
  keep the sign bit as "-nan", and still take %+ ("+inf") — the
  pre-fix engine could never deliver a nan operand to the renderer

R6.2 residue. builtins/command.def:95-96: 'if (list == 0) return
EXECUTION_SUCCESS' — the no-operand early return precedes the describe
loop. The executor's command -v/-V path (type_builtin.rs) fell through
to the any_found accumulator and returned 1 for bare 'command -v'; now
it takes the early success. The substitution entry point
(command_substitution_values.rs) switches from an all-found accumulator
to GNU's any_found rule (command.def:111-121) and routes the -V
per-operand "not found" diagnostic to stderr (oracle:
$(command -V echo nosuch) captures the description on stdout, prints
"command: nosuch: not found" on stderr, rc 0).

Verification: WSL GNU bash 5.3.0 (/usr/local/bin/bash) oracle,
script-file byte compare over a 110-case matrix (ticket reproforms plus
leading/trailing-white-space, suffix, radix-prefix, overflow, char-
constant, float-subject and command -v operand shapes): 107/110 byte
identical. The 3 remaining are pre-existing engine divergences outside
this facet, reproducible on master: '%f' 1e999 (oracle strtofltmax
long-double expansion vs f64 inf) and '%a' mantissa style (0xap-1 vs
0x1.4p+2; 'printf %a 5' diverges on master unchanged).

Tests: src/builtins/printf_tests.rs mod numeric_validation_704 (14
cases); tests/regression.rs issue_704 gains 704.2b no-operand success
and 704.2c substitution any-found + -V stderr. printf family 52 green,
command -v family green. Full suite --test-threads=1 --no-fail-fast:
failure set identical to pristine master (34 machine-state failures,
0 new). cargo fmt clean; clippy clean on all touched code.
@caomengxuan666

Copy link
Copy Markdown
Member Author

独立评审结论:APPROVE-WITH-NITS

评审对象 9ced20a(= origin/fix/704,含 merge bd24cf8,merge-base==origin/master,merge 零带入),base bd24cf8。评审在独立 worktree rubash-cr-869 实做,未触碰其他树。

1. GNU 契约核对(vendored bash @b4608166,逐坐标实读)

  • printf.def:1387-1398 chk_converror():*ep || ep == s 分支先于 errno == ERANGE —— 实现"后缀优先于 ERANGE"(含 99999999999999999999x → invalid number 而非 range)一致 ✓
  • printf.def:1427 getintmax / :1450 getuintmax / :1472-1473 getdouble:strtoimax/strtoumax/strtod、base 0、引号先行 asciicode()、POSIX.2 accumulated-value 保值语义 ✓
  • common.c:213-226 sh_invalidnum():octal='0'+digit、hex='0'+'x'(仅小写 x)—— invalid_number_error 镜像一致,含 0Xg→"invalid number" 的边角 ✓
  • printf.def:1520-1543 asciicode():孤引号 → arg[1] NUL、无诊断 ✓
  • command.def:96-97:if (list == 0) return (EXECUTION_SUCCESS); 无操作数早退 ✓;:110-121 any-found 累加 + verbose != CDESC_REUSABLE 的 -V-only 诊断 ✓(替换入口与直连路径两侧都改齐)
  • printf.def:779-808:确认 %e/%f/%g/%a 默认走 getfloatmax()/strtofltmax(long double),getdouble 仅 posixly_correct —— 已注明的 1e999 分歧根因归档正确 ✓

2. WSL GNU bash 5.3.0(glibc 2.39)双侧逐字节 oracle(118 形,stdout+rc+stderr 正文)

  • 110 形全等:前后空白混合('5 '/ 5/\t\t 5)、0xg/0Xg/018/08、+ 5/--5、'5 x、孤引号与多字符引号形、'ä(228)、ERANGE 饱和(含 %u -1 回绕、%u -99999… 不取负)、0b101/0b/0B1、nan(123)/nan(12x、5e/5e+/5./.5/5.5.5、0x1p3/0x.8p1/-0x1p-2/0x1g、%*d 宽度操作数 5x、多操作数续跑保值、空白全操作数、%F 有限值/-0.0、inf/nan 大小写矩阵(%F→INF/NAN、-nan、%a inf、%e inf、%g -INF)
  • 8 形分歧、全部三类、均为 master 原样(零回退):
    1. %f 1e999 long-double(PR 已注明);
    2. %a/%A 尾数风格 ×3(PR 已注明);
    3. 新发现(PR 未列):infin/nann/infx/nanx → GNU 打 inf/nan + invalid,本 PR 打 0.000000 + invalid(rc 与 stderr 正文逐字节全等)。master 的 parse::<f64>() 对这些形输出逐字节相同 → 非回退;但 PR 注释称 inf/nan 拼写已覆盖,此"词前缀 + 尾字母"子类(strtod 最长前缀仍取 inf/nan)未覆盖。建议 follow-up issue,不阻塞。

3. 重写面回归与性能

  • printf 全家族 16 形(%s/%q/%b/%c、宽度精度、\x41/\101/\u4e2d、%%、flags)双侧全等 ✓;性能抽验:200×%d 循环 0.175s(GNU 同环 0.236s)、长溢出操作数即时饱和,无劣化 ✓

4. command -v/-V/-p/type

  • 21 形 oracle:裸 command -v/-V rc0、-v echo nosuch any-found rc0、$() 内 -V not-found 诊断到外层 stderr 且描述仍被捕获、type 保持 all-found rc1、-p 组合与 -v -p/-p -v -V/-- 旗标序 —— 20/21 全等;唯一差异 command -p -v ls 解析到不同 ls 绝对路径 = 本机 PATH 环境差异(两侧均 rc0 找到)✓

5. 测试与卫生

  • 新增 15 例 printf_tests + 704.2b/c 自跑全过;printf lib 52/52 ✓;regression.rs 77 过 + upstream_suite_slice_snapshots 失败(机器态,见下)
  • 全量 --no-fail-fast --test-threads=1(312 二进制):fix/704 失败 36;master 基线同法 36;双向失败集逐名完全相等(comm 两侧皆空) —— 零新增失败。失败集均为机器态类(fd_redirects c_external ×10、examples ×10、tilde_user ×3、command -p PATH 类 ×3、sigint/sigterm、upstream_suite_slice 等)
  • cargo fmt --check ✓;RUSTFLAGS=-D warnings cargo check --all-targets 3 错均在 PR 未触碰的既有测试文件(issue282/issue531/issue804,master 同样存在)
  • 夹带扫描:PR 文件集恰 7 个,全在声明范围;merge 提交相对 origin/master 零差异 ✓

6. GPL 红线

number.rs/float.rs/value.rs 为原创 Rust 实现;注释仅 file:line 坐标与行为引用,无 C 结构/表达式镜像 ✓

NITS(不阻塞)

  1. inf/nan 词前缀子类(上述第 3 类分歧)建议开 follow-up;
  2. 注释坐标 ±1 漂移:command.def:95-96 实为 96-97、asciicode 实为 1520-1543(getintmax/getuintmax/chk_converror 坐标精确)。

不合并,留给 maintainer。

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.

builtins/expansion: printf numeric validation, command -v multi-operand status, ~+/~-/~user in assignments, type -a ordering, BASH_ARGC/HISTCMD

1 participant