Repository navigation
fix(cat): open the redirect target once in the streaming cat paths - #447
Merged
Merged
Conversation
`write_cat_output` re-created (`File::create`) the `>` target on every call, and the chunked paths (`stream_inherited_cat`, `stream_cat_stdin_operand`) called it once per 8 KiB block — so `cat > out` with an inherited pipe kept only the last block (745 of 82665 bytes, rc 0, stderr empty). GNU cat.c opens the output once; `cat >> out` and `cat < file > out` were unaffected because they append or write once. Adds `Executor::open_cat_output_sink` (single open for the whole copy; `/dev/*` fd aliases still resolve through the fd table per chunk) and uses it in both streaming loops. Regression test: tests/cat_redirect_stream_regressions.rs (niubash#198).
Contributor
Author
|
Baseline leg — same workflow, second job, upstream So the before/after pair is 745 → 82665 with only the engine revision differing. Fixed build's control legs: (The PR's own CI checks are pending maintainer approval — GitHub holds workflow runs from a first-time fork contributor until someone approves them.) |
Member
|
只是cat有这个情况吗?tail,head不会吗?能否验证一下,如果他们两个也有,那么或许可以一起修复一下 |
Member
|
维护者复核完毕,质量到位,已 approve:
|
caomengxuan666
added a commit
that referenced
this pull request
Oct 9, 2026
…d, 5 new engine bugs filed (#460 #461 #462 #463 #465), #435/#436/#425 confirmed fixed on fresh master Findings doc + machine-readable triage JSON (139 hashes / 172 rows). Also includes whitespace-only 'cargo fmt' normalization of src/executor/external_finish.rs and tests/cat_redirect_stream_regressions.rs: those files landed on master in b85811b (#447) under a different rustfmt and fail the repo pre-commit hook on any commit (main worktree @a157656d fails 'cargo fmt --check' identically). No semantic changes. Lane: wt98b/goldtri (triage only; no product changes). GNU-side evidence is the recorded harvest oracle (WSL bash 5.3.0, 2026-10-04/05) — live WSL down machine-side during this lane (WslService stopped, no admin); noted in the doc.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Symptom
cat > filefed from a parent process pipe (inherited stdin) kept only the last ~4 KiB block of the stream — rc 0, stderr empty, silent truncation:cat > outcat >> fileandcat < file > filewere unaffected.Root cause
Executor::write_cat_output(src/executor/external_finish.rs) re-creates the>target on every call (create_redirect_output→File::create, i.e. truncate). The chunked cat paths —stream_inherited_catandstream_cat_stdin_operand(src/executor/external_file_builtins.rs) — call it once per 8 KiB block, so each block truncated the previous one. GNUcat.copens the output once. The one-shot paths (cat < file > out, formattedcat -n > out) call the writer once, which is why they looked fine.Fix
Executor::open_cat_output_sink— opens the>/>>target once for the whole copy; fd aliases (/dev/stdout,/dev/fd/N) still resolve through the fd table per chunk, and the no-redirect stdout path is unchanged.tests/cat_redirect_stream_regressions.rs— chunked-stdin>keeps every byte;>>and<file >stay whole.Verification
Cloud-built on
windows-latest: niubash checked out with this branch as its[patch] ../rubashpath dependency, same Python driver, only the engine revision differs — run 37875117138:The same workflow's second job rebuilds niu.exe against upstream
masterand shows the tail-fragment baseline.Impact
Found through niubash#198:
cat > "$tmp"is exactly how Hermes Agent'swrite_file/patchpersist a file (payload on stdin), so every write above ~4 KiB kept only its tail and silently corrupted files (aconfig.pywas destroyed this way, restored from git). Any parent process feedingniu -c "cat > f"was affected.