Repository navigation
fix(checksums): read stdin in binary mode so 0x1A no longer truncates input - #1199
Merged
Merged
Conversation
… input md5sum, sha1sum, sha256sum and cksum hashed only the bytes up to the first 0x1A (Ctrl-Z) when stdin came from a pipe or a '<' redirect, and cksum's byte count shrank with it, because fd 0 uses the CRT's text mode by default and fread/std::cin stop at a text-mode EOF byte. The file-operand form already opened std::ifstream/std::ios::binary, which is why it was byte-correct. Fix: switch stdin to _O_BINARY on the exact paths these tools read, matching the idiom cat/wc/tr already use, without touching global defaults: - md5sum/sha1sum/sha256sum calculate(): before the fread(stdin) loop - cksum run(): once, covering read_file, read_crc_file and the portable_digest::hash_file_hex std::cin route used by -c/'-a' All four now match GNU coreutils hashes on 'AAAA<0x1A>BBBB\n' for the pipe, '<' redirect and '-' operand forms; NUL-byte payloads which were already correct stay correct, and plain text pipes hash unchanged. New regression tests in all four suites reference #1195.
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 #1195.
Summary
md5sum,sha1sum,sha256sumandcksumsilently truncated stdin at the first0x1A(Ctrl-Z) byte when input arrived through a pipe or a<redirect, hashing only the bytes before it. The file-operand form was correct, which pinned the bug to the stdin path: fd 0 uses the CRT's default text mode, wherefread()/std::cintreat0x1Aas end-of-file.Root cause
The digest tools never switched fd 0 to binary mode, while the pass-through tools that handle
0x1Acorrectly (cat,cut,tr,wc,od,diff…) all call_setmode(_fileno(stdin), _O_BINARY)— e.g.src/commands/cat.cpp:113.The truncated reads:
src/commands/md5sum.cpp,src/commands/sha1sum.cpp,src/commands/sha256sum.cpp:fread(buffer.data(), 1, buffer.size(), stdin)insidecalculate_*()src/commands/cksum.cpp: three stdin routes —read_file()(SYSV/BSD),read_crc_file()(CRC/legacy check), andportable_digest::hash_file_hex'sstd::cinbranch (digest-aalgorithms and-ctargets of-) — all viastd::istreamFix (per-tool, no global default change)
md5sum/sha1sum/sha256sum: one_setmode(_fileno(stdin), _O_BINARY)immediately before the stdinfreadloop (covers the-operand and the check-mode hash step, which reuse the same branch).cksum: one_setmodecall at the top ofrun()— a process-wide fd-mode switch covers all three stdin routes; fixing onlyread_file/read_crc_filewould leavecksum -a md5 -andcksum -chashes still text-mode.Evidence, before / after (
AAAA<0x1A>BBBB\n, 10 bytes)Reference: GNU coreutils (Git-for-Windows
D:/Git/usr/bin/*sum.exe).098890dde069e9abad63f19a0d9e1f32 *-(md5 of "AAAA", 4 bytes)60f5ffbcfe8a049be21dc9d6c334c82c60f5ffbc…< f098890dde069e9abad63f19a0d9e1f3260f5ffbcfe8a049be21dc9d6c334c82c-operand098890dde069e9abad63f19a0d9e1f3260f5ffbcfe8a049be21dc9d6c334c82c60f5ffbcfe8a049be21dc9d6c334c82c(already correct)< f/-e2512172abf8cc9f67fdd49eb6cacf2df71bbad3(truncated)c48bfe07cbde3bed26cc431a6dd97a90d6b7eefd< f/-63c1dd951ffedf6f7fd968ad4efa39b8ed584f162f46e715114ee184f8de9201(truncated)947cd8c9aeb7e77eaf23c65ce58e442ac3d04f4ab249599efa541b46c239662d< f/-1257162693 4(CRC and count truncated to 4 bytes)2873339409 10NUL payload
AB<NUL>CDwas already correct before and after — 5-byte hash580dfde355cb3ceb91f01544d9918f9c(md5) matches GNU for all forms on both sides. Plain-text sanityhi | md5sum→764efa883dda1e11db47671c4a3bbd9e, unchanged. Exit code on a stdin payload containing0x1A: GNU processes all bytes and exits 0; niu now does the same (rc 0 both before and after — exit codes were never the failure mode).Tests
New
#issue1195tests intests/unit/{md5sum,sha1sum,sha256sum,cksum}/: 0x1A-in-the-middle pipe hash vs GNU value (and asserting the pre-fix truncated hash is absent), NUL pipe unchanged, pipe-vs-file-operand digest equality, plain-text pipe sanity. Covered all four tools (16 tests).Only the checksum suites were run, per the issue scope.
Adjacent, noticed but intentionally not fixed here (same class of bug, share the identical path)
Repro'd on the installed 1.1.12 binaries — these were not in the issue's scope, so this PR leaves them alone; a follow-up issue can cover them:
sha512sum,sha384sum,sha224sum,b2sum,sum: identical pipe/redirect truncation at0x1Aon stdin.sha224sum/sha384sum/b2sumread stdin through the shared helperportable_digest::hash_file_hex(src/utils/digest.cppm:1057,std::cinwithout_O_BINARY) — fixing the helper would cover the three of them at once, but touches tools outside md5sum/sha1sum/sha256sum/cksum silently truncate stdin at 0x1A (Ctrl-Z) - hashes the wrong data #1195 and is deliberately deferred.cksum's check-mode reads check lists throughstd::cinas text; that matches GNU's text semantics for checksum files, left as is.The secondary cosmetic item from the issue (
*-binary marker / spacing vs GNU's-) is unchanged by this PR.