Skip to content

fix: fast-path differential divergences #707/#708/#709 (tr carriers, head -n parsing, dirname/basename edges) - #718

Merged
caomengxuan666 merged 3 commits into
masterfrom
fix/707-tr-carrier-classes
Oct 17, 2026
Merged

caomengxuan666 merged 3 commits into
masterfrom
fix/707-tr-carrier-classes

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Fixes three confirmed fast-path divergences found by the differential audit in #706.

#707 — tr inline arm lets carried control bytes escape its classes

The inline tr arms ran set membership over the shell's transport text. Carrier bytes (0x0C, 0x1B, 0x14..0x1F) travel as U+E000+U+E0xx marker pairs, so [:cntrl:]/[:space:] never matched them. translate_tr now decodes once (shell_text_to_raw_bytes) before matching and re-encodes the result, fixing both the pipeline arm and the command-substitution arm at their shared boundary.

shape before after GNU
printf 'a\033b\nc\n' | tr '[:cntrl:]' X a\x1bbXcX aXbXcX aXbXcX
printf 'a\014b\n' | tr '[:space:]' _ a\x0cb_ a_b_ a_b_
printf 'a\013\014b\n' | tr '[:cntrl:]' X aX\x0cbX aXXbX aXXbX

#708 — head inline arm silently defaults invalid/missing/negative -n

The arms skipped every -… word and fell through head_line_count(...).unwrap_or(10), so a malformed count printed the first ten lines with rc 0 and GNU's negative form was lost. A strict parse_head_line_count shared by the pipeline and command-substitution arms now keeps only representable count forms inline; everything else declines to the real head.

shape before after external / GNU
head -n -1 all 3 lines, rc 0 a\nb\n a\nb
head -n abc 3 lines, rc 0 error, rc 1 error, rc 1
head -x 3 lines, rc 0 error, rc 1 error, rc 1
head -n 3 lines, rc 0 error, rc 1 error, rc 1
head --lines 3 lines, rc 0 error, rc 1 error, rc 1
x=$(… | head -n -1) all 3 lines a\nb a\nb

#709 — dirname/basename string edges

trim_trailing_slashes collapsed both // and '' to /. dirname now implements GNU mdir_name/last_component/dir_len (preserving a leading // but not ///), and basename returns the empty string for an empty operand. The all-slash basename spelling stays / because that is what the external WinuxCmd fallback reports (a shared divergence, explicitly out of scope in #709).

shape before after GNU
dirname // / // //
dirname //a / // //
dirname '' / . .
basename '' / (empty) (empty)

Harness delta (#706, run from origin/audit/fast-path-differential, not modified)

Run against a build of this branch (the known list still carries the old expectations, so the harness reports each fixed case as FIXED?):

The four path-translation dirname rows (dirname_simple, dirname_letter, dirname_trailing, dirname_letter_double) already pass on current origin/master (#649); the committed known annotations reflect a stale installed binary and are not caused by this change.

Full harness: no new regressions. The single reported grep_utf8_i divergence reproduces identically on pristine origin/master (verified with a clean worktree build) and is unrelated to these files.

Tests

Added tests/issue707_tr_carrier_classes.rs, tests/issue708_head_option_parsing.rs, tests/issue709_dirname_basename_edges.rs covering every shape above plus a no-regression case for each command's ordinary path.

cargo test --test issue707_tr_carrier_classes --test issue708_head_option_parsing --test issue709_dirname_basename_edges
  13 passed; 0 failed

cargo test --lib translate_tr
  1 passed; 0 failed

cargo fmt --check
  clean

Found but left alone

)

GNU dirname keeps a leading // and returns . for an empty operand; basename
returns the empty string for an empty operand. The inline fast paths collapsed
all-slash and empty values to / via trim_trailing_slashes. Reimplement dirname
with the GNU mdir_name/last_component/dir_len algorithm (preserving //-but-not-///)
and special-case the empty basename operand; the all-slash basename spelling
stays / to match the external WinuxCmd fallback.
…to 10 (#708)

The inline head arms skipped every -option word and fell through
head_line_count(...).unwrap_or(10), so an unparsable count printed the first
ten lines with rc 0 and GNU's negative form (-n -1 = all but the last line)
was lost. Add a strict parser shared by the pipeline and command-substitution
arms: only -n N / -nN / --lines=N / --lines N / -N (non-negative) and the
negative -n -N form stay inline; -c/--bytes, unknown options, operands and a
missing operand decline to the real head, which reports the error.
The inline tr arms ran set membership over the shell's transport text.
Carrier bytes (0x0C, 0x1B, 0x14..0x1F) travel as U+E000+U+E0xx marker pairs,
so [:cntrl:]/[:space:] never matched them and the carried control bytes
escaped translation. translate_tr now decodes to the exact payload bytes
(shell_text_to_raw_bytes) before matching and re-encodes the result, so both
the pipeline and command-substitution arms are fixed at the shared boundary.
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