Skip to content

fix(special-paths): fd_alias_target numeric field is canonical decimal (rubash#1973) - #1988

Merged
caomengxuan666 merged 1 commit into
masterfrom
fix/1973-fd-alias-leading-zero
Oct 28, 2026
Merged

caomengxuan666 merged 1 commit into
masterfrom
fix/1973-fd-alias-leading-zero

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1973.

What was wrong

The special-path registry's fd_alias_target accepted leading-zero fd
spellings (/dev/fd/00, /dev/fd/03, /dev/fd/007,
/proc/self/fd/00) that GNU bash rejects on every face, and the
registry battery pinned the wrong answer as expected
(("/dev/fd/03", FdAlias(3))). On Windows this made cat /dev/fd/03 3<<< hi really read fd 3 (printed hi, rc 0) where GNU fails ENOENT,
exec 0</dev/fd/00 silently duplicated fd 0, and echo x | cat /dev/fd/00 read the pipe. The R3 pass (PR #1971) stopped its
convergence at three faces precisely because the registry itself was
the divergent side.

GNU C source (specification)

  • redir.c:601 _redir_special_filenames: the /dev/fd/[0-9]*
    interception exists only #if !defined(HAVE_DEV_FD); on the oracle
    platform (Linux) HAVE_DEV_FD is set and the literal open
    decides — procfs serves only canonical names, so the leading-zero
    family fails ENOENT everywhere.
  • redir.c:630 redir_special_open RF_DEVFD fallback requires
    all_digits(filename+8) (general.c:233) — never a sign.

Oracle probes (WSL GNU bash 5.3.20(1)-release, script files):
cat ENOENT, redirect ENOENT, test -r false, [[ -e ]] false,
source ENOENT — including with fd 3 genuinely open.

Changes (every face converged, file:line)

  1. Registry — src/special_paths.rs:127 fd_alias_target +
    new canonical_fd_number (src/special_paths.rs:150): numeric
    field is canonical decimal — ASCII digits only, no sign, no
    whitespace, no leading zero ("0" itself excepted), u32 range.
  2. Battery + pins — src/special_paths.rs tests: the wrong pin
    replaced by the GNU-correct Filesystem answers for the whole
    leading-zero family (and the sign spellings kept pinned); legacy
    mirror updated to the corrected judgment so the drift trip-wire
    stays live; new per-spelling pin
    issue1973_leading_zero_fd_spellings_are_not_aliases.
  3. Face (a) — src/builtins/test.rs:619 (virtual_device_test
    fd-0 table) and :635 (fd-1/2 table): literal tables → registry
    queries (fd_alias_target == Some(0) / Some(1)|Some(2)).
  4. Face (b) — src/builtins/source.rs:178 fd-0 arm →
    fd_alias_target(filename) == Some(0).
  5. Face (c) — src/executor/trap_exec.rs:3453 exec-redirect fd-0
    arm → fd_alias_target(&target) == Some(0).
  6. New behavior pins —
    tests/issue1973_leading_zero_fd_aliases_regressions.rs (5 tests,
    one per face + controls).

Convergence is lossless after (1): the registry's accepted spelling
sets for fds 0/1/2 are exactly the literal tables' sets.

Before / after vs GNU 5.3.20 (script-file probes)

case rubash before rubash after GNU 5.3.20
cat /dev/fd/03 3<<< hi hi rc=0 (read fd 3) ENOENT rc=1 ENOENT rc=1
cat /dev/fd/007 7<<< hi hi rc=0 (read fd 7) ENOENT rc=1 ENOENT rc=1
echo body | cat /dev/fd/00 body rc=0 (read fd 0) ENOENT rc=1 ENOENT rc=1
. /dev/fd/00 <<< text ENOENT rc=1 ENOENT rc=1 ENOENT rc=1
exec 0</dev/fd/00 silent success rc=0 ENOENT rc=1, script continues same (probed)
[[ -e /dev/fd/00 ]] / test -r /dev/fd/03 false / rc=1 false / rc=1 false / rc=1
echo hi | cat /dev/fd/0, . /dev/fd/0 <<< …, [[ -e /dev/fd/0 ]], . /proc/self/fd/0 <<< … work work work
cat /dev/fd/+3 3<<< hi ENOENT rc=1 ENOENT rc=1 ENOENT rc=1

After the fix, rubash matches GNU on every semantic line; the only
probe diffs are diagnostic path prefixes.

Tests

cargo test --release, CARGO_TARGET_DIR=D:/repo/nbbuild/pkg2/target,
baseline 7f51922:

  • special_paths lib: 3/3 (battery + new pin)
  • dev_fd_operands lib (R3/D1 pins): 2/2
  • new issue1973_leading_zero_fd_aliases_regressions: 5/5
  • touched-face files: issue1642 13/13, issue700 5/5, issue938 3/3,
    issue396 1/4*, issue1373 3/3, issue355 10/10, issue429 8/8,
    issue1557 (0 tests, Windows-empty)
  • regression.rs -- r3_: 5/6*

* A/B-verified failing identically at pristine 7f51922 on this
machine — environment-bound (pipe-lifetime stderr noise; external
cat not resolvable in the test child's PATH), not caused by this
change. issue1708 does not compile on Windows (Unix-only imports),
excluded.

Disclosure

cargo fmt fails at master scale (os error 206), so the pre-commit fmt
hook cannot run: committed with --no-verify. rustfmt --edition 2021
was run explicitly on the five touched files; --check passes clean
on them.

…l (rubash#1973)

The special-path registry accepted leading-zero fd spellings
(/dev/fd/00, /dev/fd/03, /dev/fd/007, /proc/self/fd/00) that GNU bash
rejects on every face. On Windows, `cat /dev/fd/03 3<<< hi` really read
fd 3 (printed "hi", rc 0) where GNU 5.3.20 fails ENOENT even with fd 3
genuinely open; `exec 0</dev/fd/00` silently duplicated fd 0 where GNU
prints ENOENT; `echo x | cat /dev/fd/00` read the pipe where GNU fails
ENOENT. The wrong registry answer was pinned as "expected" by the
special_paths battery, and the R3 convergence pass stopped at three
faces that still carried literal fd-0 tables.

GNU C source (specification):
- redir.c:601 _redir_special_filenames: the /dev/fd/[0-9]* interception
  exists only #if !defined(HAVE_DEV_FD); on the oracle platform (Linux)
  HAVE_DEV_FD is set and the literal open decides. procfs serves only
  canonical names (no leading zeros), so the leading-zero family fails
  ENOENT everywhere (issue #1973 oracle probes, bash 5.3.20(1)-release:
  cat ENOENT, redirect ENOENT, test -r false, [[ -e ]] false, source
  ENOENT).
- redir.c:630 redir_special_open RF_DEVFD fallback:
  all_digits(filename+8) && valid_number && lfd == (int)lfd;
  all_digits (general.c:233) never admits a sign.

Changes:
- src/special_paths.rs fd_alias_target: numeric field now parsed by a
  canonical_decimal helper — ASCII digits only, no sign/whitespace, no
  leading zero ("0" itself excepted), u32 range. Battery pin
  (/dev/fd/03 -> FdAlias(3)) replaced with the GNU-correct
  Filesystem answers for the whole leading-zero family plus sign
  spellings; legacy mirror updated to the corrected judgment so the
  drift trip-wire stays live; new per-spelling regression pin
  issue1973_leading_zero_fd_spellings_are_not_aliases.
- Face convergence to fd_alias_target(..) == Some(0) / Some(1)|Some(2),
  lossless after the registry fix because the accepted spelling sets
  are unchanged:
  - src/builtins/test.rs virtual_device_test: fd-0 and fd-1/2 literal
    tables (was test.rs:619 and :631) now query the registry.
  - src/builtins/source.rs:178 fd-0 arm now queries the registry.
  - src/executor/trap_exec.rs:3453 exec-redirect fd-0 arm now queries
    the registry.
- tests/issue1973_leading_zero_fd_aliases_regressions.rs: behavior pins
  for all three faces (cat operand, pipeline, source, exec redirect,
  test/[[), expectations from GNU 5.3.20 probes.

Verification (WSL GNU bash 5.3.20, script files):
- probe scripts + raw outputs under D:/repo/nbbuild/pkg2/
  (gnu-probe-results.txt vs before-results.txt vs after-results.txt):
  after the fix rubash matches GNU on every semantic line; the only
  diffs are diagnostic path prefixes.
- cargo test --release (CARGO_TARGET_DIR=D:/repo/nbbuild/pkg2/target):
  special_paths lib 3/3, dev_fd_operands lib 2/2, new issue1973 file
  5/5, issue1642 13/13, issue700 5/5, issue938 3/3, issue1373 3/3,
  issue355 10/10, issue429 8/8, regression.rs r3_ 5/6.
- Pre-existing on this machine (A/B-verified against pristine 7f51922,
  not caused by this change): issue1467 (Broken pipe stderr noise),
  issue396 3/4, regression r3_external_operand_plus_fd_spelling_rejected
  (external cat not found in the test child's PATH). issue1708 does not
  compile on Windows (Unix-only imports), excluded.

Disclosure: `cargo fmt` fails at master scale with os error 206
(filename too long), so the pre-commit fmt hook cannot run; committed
with --no-verify. rustfmt --edition 2021 was run explicitly on the five
touched files and `--check` passes clean on them.
@caomengxuan666
caomengxuan666 marked this pull request as ready for review October 28, 2026 23:31
@caomengxuan666
caomengxuan666 merged commit 2f83fb0 into master Oct 28, 2026
11 checks passed
@caomengxuan666
caomengxuan666 deleted the fix/1973-fd-alias-leading-zero branch October 30, 2026 01:41
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.

special_paths registry defect: fd_alias_target accepts leading-zero fd spellings (/dev/fd/00, /dev/fd/03) that GNU rejects everywhere

1 participant