Repository navigation
fix(dd): enforce a declared file-size limit at write time (ulimit -f / RLIMIT_FSIZE) - #1455
Merged
Merged
Conversation
dd ignored the file-size limit its POSIX parent had declared: under `ulimit -f 0` the GNU dd child dies by SIGXFSZ (rc 153, shell prints "File size limit exceeded"), while WinuxCmd dd wrote the full output and exited 0, so the death a shell must notice never happens (reproduced by rubash tests/issue1493_subshell_signal_notice_regressions.rs against WinuxCmd 1.1.20). Windows has no setrlimit()/RLIMIT_FSIZE, so the limit has to be declared by the parent through the environment. dd now honors two channels, in precedence order: - WINUXCMD_RLIMIT_FSIZE=<bytes> - the canonical opt-in contract (decimal bytes; "unlimited"/empty disables; a malformed value warns once and disables). A winuxsh-side `ulimit` can adopt it later. - __RUBASH_ULIMIT_F=<soft>:<hard> - rubash's Windows ulimit emulation fork-inheritance channel (bytes, 18446744073709551615 = RLIM_INFINITY); the soft value is taken as the RLIMIT_FSIZE soft limit, so `ulimit -f 0; exec dd ...` reaches dd today. Enforcement mirrors the kernel's RLIMIT_FSIZE write semantics (GNU coreutils dd under setrlimit, Linux): writes are metered against the output file's frontier; the crossing write is delivered only up to the limit and dd then dies with the SIGXFSZ shape - no diagnostic, no transfer summary, exit 128+25 = 153. A write ending exactly at the limit is legal. Only regular-file outputs are covered (GetFileType == FILE_TYPE_DISK), matching Linux where pipe writes are not subject to RLIMIT_FSIZE. The EFBIG-after-caught-SIGXFSZ shape (diagnostic + summary, exit 1) needs the parent's trap state, which has no channel, and is documented as not reproduced. Unit tests: tests/unit/dd/dd_unit_test.cpp (8 new cases: zero cap through both channels, crossing-write truncation, exact-at-limit, unlimited literal, malformed value, no-limit regression guard, pipe exemption).
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.
Closes #1453.
What
ddnow enforces a file-size limit declared by its POSIX parent, closing the gap wheredd.exeignoredulimit -f 0and wrote the full output with exit 0 while GNUdddies bySIGXFSZ(rc 153, shell printsFile size limit exceeded).Why the parent declares it through the environment
Windows has no
setrlimit()/RLIMIT_FSIZE. The limit has to travel parent→child explicitly. Today:ulimitsupport at all (noulimit/RLIMITreferences in the repo), so it declares nothing.__RUBASH_ULIMIT_F=<soft>:<hard>in decimal bytes with18446744073709551615= RLIM_INFINITY (rubash src/builtins/ulimit.rs, platform-contract note lines 36-58, key format lines 326-338 — the value stored is the setrlimit-semantic byte count, block factor already applied).So dd honors two channels, in precedence order:
WINUXCMD_RLIMIT_FSIZE=<bytes>— the canonical, documented WinuxCmd contract (opt-in;unlimited/empty disables; a malformed value warns once and disables). This is what a future winuxsh-sideulimitshould set.__RUBASH_ULIMIT_F— rubash's existing channel, soulimit -f 0; exec dd ...under rubash reaches dd with zero parent-side changes.GNU semantics reproduced (coreutils dd + setrlimit(RLIMIT_FSIZE), Linux)
GetFileType(hOut) == FILE_TYPE_DISKonly — Linux does not applyRLIMIT_FSIZEto pipe writes, sodd | catkeeps working).128+25 = 153— the parent shell reports the notice,$?is 153.EFBIG-after-caught-SIGXFSZ shape (dd: error writing 'FILE': File size limit exceeded+ summary, exit 1) needs the parent's trap state, which has no channel on Windows.Tests
8 new unit tests in
tests/unit/dd/dd_unit_test.cpp(winuxcmd-tests): zero cap through both channels (silent stderr, rc 153, empty file), crossing-write truncation (cap 10, obs 4 → exactly 10 bytes land), exact-at-limit success, unlimited literal, malformed value warn-and-disable, no-limit regression guard, pipe exemption.Manual evidence:
DDevidence script over the release build —WINUXCMD_RLIMIT_FSIZE=0 dd if=/dev/zero of=big bs=1024 count=100→ rc 153, empty stderr,bigsize 0 (GNU: rc 153 under WSL bash 5.3 withulimit -f 0).Notes for reviewers
flush_output_bufferreturns a tri-state now (ok/error/sigxfsz); both call sites handle the death shape by returning 153 immediately without printing stats, matching a signal kill (GNU dd prints nothing when SIGXFSZ kills it).seek=/truncate handling, soseek=offsets count toward the cap (ftruncate-style seek truncation is not limited, matching Linux whereftruncatebypasses RLIMIT_FSIZE).