Skip to content

sha512sum/sha384sum/sha224sum/b2sum/sum truncate binary stdin at 0x1A (same text-mode root as #1195; shared hash_file_hex path) #1200

Closed

Description

@caomengxuan666

Summary

Follow-up to #1195 / #1199. sha512sum / sha384sum / sha224sum / b2sum / sum silently truncate stdin at byte 0x1A (Ctrl-Z) — the same fd-0 text-mode read the four tools fixed in PR #1199 had. All five hash only the bytes before the first 0x1A; the file-operand form is byte-correct.

Environment: winuxcmd 1.1.12 (Niubash 1.4.0) — Windows 10 x64, installed binaries at .../Niubash/winuxcmd/usr/bin/*sum.exe.
Method: differential vs GNU coreutils (D:/Git/usr/bin/*sum.exe, Git-for-Windows).
Payload: AAAA\032BBBB\n — 10 bytes, 0x1A in the middle.

Repro

$ printf 'AAAA\032BBBB\n' > f
$ cat f | sha512sum   # pipe — also identical via `< f` and `- < f`
53b74be8…8444cf8 *-   # == sha512("AAAA"), 4 bytes — WRONG (truncated at 0x1A)
$ sha512sum f         # argument
16d6cea3…a6febab *f   # correct, full 10 bytes

Before / after (v1.1.12 stdin forms vs GNU on the same bytes). GNU reference values computed with D:/Git/usr/bin:

tool form before (v1.1.12) after == GNU expected
sha512sum pipe / < f / - < f 53b74be8b295b733fdfafbd7d2a22b1686733740de7fdc592b26cf3e1874cfce158170ce9230e24696331a61829244e5d9f48abdacc9ffa8c4cb498724844cf8 *- (truncated, sha512 of AAAA) 16d6cea306afa32e67877b65e6d005f9f238ae85f15359165697b75d8d682358e888acec532856591ce894f4079d625bc3a981070b5f4ae879254b253a6febab
sha384sum pipe / < f / - < f d8bf1572ab3b9bc239325d2a657ad37cb8fa6cb32c2d83cddee33be1238eb7a40b33d1bc796b426b6c68c22e4769dea2 *- (truncated) dc7e5641bd3d50fb39f132ac116fd803b986e8fd3ae70752642aeff2ea1f08c24cce4038312a542441ecd6daf4449d10
sha224sum pipe / < f / - < f 5cdde10ca4df3e7e6e0e428f683e1c6ca0bfb917555ad94727a3f344 *- (truncated) 3afda03a96d961663239a82aa7416fb39446399964417f7b8c6c472d
b2sum pipe / < f / - < f 5448fb61bfeba39eb14a49f72310ab14653b230655b0adccadcb31148602317af3bcef80f09f0945f06607465a0e467ab8cf257a3088d25065aded9fc0963271 *- (truncated) 79d9a3f0d2c8368f371b84ca8557412f5d4377f7c40d85be8f7c3829608c52408642112674ca24ce035013ba3408a9ce532ad94432bbff005e5f57a17424bf14
sha512sum file operand 16d6cea3…a6febab *f (already correct) unchanged
sha384sum file operand dc7e5641…a1f08c24 *f (already correct) unchanged
sha224sum file operand 3afda03a…4c6c472d *f (already correct) unchanged
b2sum file operand 79d9a3f0…424bf14 *f (already correct) unchanged
sum (default BSD) pipe / < f / - < f 57465 1 (truncated; - form prints 57465 1 -) 38858 1
sum -s (SysV) pipe / < f / - < f 260 1 (truncated) 560 1
sum file operand 38858 1 / 560 1 f (already correct) unchanged

The truncated digests all equal the hash of the first 4 bytes (AAAA) — verified against GNU (printf 'AAAA' | sha512sum → 53b74be8…), so no 0x1A-trailing bytes ever reach the hasher.

Already correct before this fix (not broken):

  • NUL payload AB<NUL>CD (5 bytes): sha224sum pipe / < give 0a2bf2317b9595e5a591a754b51dc5aa9f37bd3ab4e24e9f21fafff2 *- on both niu v1.1.12 and GNU; sum gives 53361 1 on both. Only the specific byte 0x1A triggers it.
  • Exit codes: 0 before and after — the failure mode is silent wrong output, not an error.
  • Plain-text pipes: unaffected.

Root cause

Same as #1195: fd 0 carries the CRT's default text mode, and fread/std::cin treat 0x1A as end-of-file. But this family's stdin reads live in different places than the four PR #1199 fixed:

  • sha384sum / sha224sum / b2sum: the shared helper portable_digest::hash_file_hex — std::istream* input = &std::cin; at src/utils/digest.cppm:1060, with no _setmode(_fileno(stdin), _O_BINARY) before the std::cin-based streaming loop (already-binary std::ifstream file operands are fine).
  • sha512sum: its own Windows CryptoAPI path — fread(buffer.data(), 1, buffer.size(), stdin) at src/commands/sha512sum.cpp:206, un-binary (the helper does not cover it).
  • sum (SysV CRC and BSD rot-word): std::istream* input = &std::cin; in calculate_checksum() at src/commands/sum.cpp:87, un-binary; the tool never goes through hash_file_hex, so it needs its own _setmode.

The pass-through tools that handle 0x1A correctly all call _setmode(_fileno(stdin), _O_BINARY) (e.g. src/commands/cat.cpp:113), and file_io does the same before reading std::cin (src/utils/file_io.cppm:259); the digest-family paths above simply never made that switch.

Scope note (relation to #1195 / #1199)

PR #1199 deliberately fixed only the four tools named in #1195 — md5sum, sha1sum, sha256sum, cksum — touching only their per-tool files, and explicitly deferred this family. Key difference from #1195:

  • One change to the shared helper hash_file_hex (src/utils/digest.cppm, set fd 0 to _O_BINARY before the std::cin branch) cures sha384sum + sha224sum + b2sum at once (it also feeds the cksum -a/-c portable-branch stdin route).
  • sha512sum (src/commands/sha512sum.cpp) and sum (src/commands/sum.cpp) read stdin in their own files and each need a local _setmode alongside it.

No global fd-0 default change is proposed, matching the #1199 approach.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugcompat-gap

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions