Skip to content

fix(checksums): read stdin in binary mode so 0x1A no longer truncates input - #1199

Merged
caomengxuan666 merged 1 commit into
mainfrom
fix/1195-checksum-binary-stdin
Oct 17, 2026
Merged

caomengxuan666 merged 1 commit into
mainfrom
fix/1195-checksum-binary-stdin

Conversation

@caomengxuan666

Copy link
Copy Markdown
Member

Closes #1195.

Summary

md5sum, sha1sum, sha256sum and cksum silently truncated stdin at the first 0x1A (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, where fread()/std::cin treat 0x1A as end-of-file.

Root cause

The digest tools never switched fd 0 to binary mode, while the pass-through tools that handle 0x1A correctly (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) inside calculate_*()
  • src/commands/cksum.cpp: three stdin routes — read_file() (SYSV/BSD), read_crc_file() (CRC/legacy check), and portable_digest::hash_file_hex's std::cin branch (digest -a algorithms and -c targets of -) — all via std::istream

Fix (per-tool, no global default change)

  • md5sum / sha1sum / sha256sum: one _setmode(_fileno(stdin), _O_BINARY) immediately before the stdin fread loop (covers the - operand and the check-mode hash step, which reuse the same branch).
  • cksum: one _setmode call at the top of run() — a process-wide fd-mode switch covers all three stdin routes; fixing only read_file/read_crc_file would leave cksum -a md5 - and cksum -c hashes still text-mode.

Evidence, before / after (AAAA<0x1A>BBBB\n, 10 bytes)

Reference: GNU coreutils (Git-for-Windows D:/Git/usr/bin/*sum.exe).

tool form before after GNU
md5sum pipe 098890dde069e9abad63f19a0d9e1f32 *- (md5 of "AAAA", 4 bytes) 60f5ffbcfe8a049be21dc9d6c334c82c ✅ 60f5ffbc…
md5sum < f 098890dde069e9abad63f19a0d9e1f32 60f5ffbcfe8a049be21dc9d6c334c82c ✅
md5sum - operand 098890dde069e9abad63f19a0d9e1f32 60f5ffbcfe8a049be21dc9d6c334c82c ✅
md5sum file operand 60f5ffbcfe8a049be21dc9d6c334c82c (already correct) unchanged ✅
sha1sum pipe / < f / - e2512172abf8cc9f67fdd49eb6cacf2df71bbad3 (truncated) c48bfe07cbde3bed26cc431a6dd97a90d6b7eefd ✅
sha256sum pipe / < f / - 63c1dd951ffedf6f7fd968ad4efa39b8ed584f162f46e715114ee184f8de9201 (truncated) 947cd8c9aeb7e77eaf23c65ce58e442ac3d04f4ab249599efa541b46c239662d ✅
cksum pipe / < f / - 1257162693 4 (CRC and count truncated to 4 bytes) 2873339409 10 ✅

NUL payload AB<NUL>CD was already correct before and after — 5-byte hash 580dfde355cb3ceb91f01544d9918f9c (md5) matches GNU for all forms on both sides. Plain-text sanity hi | md5sum → 764efa883dda1e11db47671c4a3bbd9e, unchanged. Exit code on a stdin payload containing 0x1A: 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 #issue1195 tests in tests/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).

winuxcmd-tests --gtest_filter='md5sum.*:sha1sum.*:sha256sum.*:cksum.*'
91 tests with filter ..., passed

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 at 0x1A on stdin.
  • sha224sum / sha384sum / b2sum read stdin through the shared helper portable_digest::hash_file_hex (src/utils/digest.cppm:1057, std::cin without _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 through std::cin as 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.

… 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.
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.

md5sum/sha1sum/sha256sum/cksum silently truncate stdin at 0x1A (Ctrl-Z) - hashes the wrong data

1 participant