head/tail: parse zero byte counts with size suffix as 0 bytes - #13706
Open
MeGaurav4 wants to merge 1 commit into
Open
head/tail: parse zero byte counts with size suffix as 0 bytes#13706MeGaurav4 wants to merge 1 commit into
MeGaurav4 wants to merge 1 commit into
Conversation
MeGaurav4
force-pushed
the
fix/head-zero-bytes-suffix
branch
from
August 2, 2026 06:45
391d862 to
93ee719
Compare
0K, 00K and similar inputs were being parsed as 1KiB because the leading zeros were stripped before suffix interpretation, leaving a bare suffix that parse_size treats as 1 unit. The leading zeros mean the value is zero, so a bare suffix must still produce 0. GNU coreutils prints nothing for head -c 0K and tail -c 0K. Closes uutils#13703
MeGaurav4
force-pushed
the
fix/head-zero-bytes-suffix
branch
from
August 2, 2026 06:49
93ee719 to
c7bde4b
Compare
Merging this PR will improve performance by 3.03%
|
| Mode | Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|---|
| ⚡ | Simulation | cksum_crc32b |
40.9 ms | 39.7 ms | +3.03% |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing MeGaurav4:fix/head-zero-bytes-suffix (c7bde4b) with main (0c8a3c7)
Footnotes
-
46 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
|
GNU testsuite comparison: |
Collaborator
|
This change makes |
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.
head/tail: parse zero byte counts with size suffix as 0 bytes, not 1KiB
head --bytes=0K,tail -c 0Kand similar inputs (00K,+0K,0M) print the whole input instead of nothing. GNU coreutils prints nothing for all of these.Closes #13703
Root cause
parse_signed_num_maxstrips leading zeros before the size suffix is interpreted, so0Kbecomes the bare suffixK. A bare suffix means "1 of that unit" inparse_size(empty numeric part parses as 1), so0Kparsed as 1KiB instead of 0 bytes.Fix
When the trimmed string contains no digits (only a suffix remains after stripping leading zeros), parse the suffix once to validate it, then return 0: a zero count times any unit is zero bytes. Invalid bare suffixes still error (e.g.
head -c 0Berrors, matching GNU, becauseBis not valid for head's parser).Affects only
head -c/--bytesandtail -c/--bytes(the only users of this parser).Verification
cargo test -p uucore --lib: 94 passed (includes 5 new unit tests inparse_signed_num)cargo test --features "head tail" test_zero_bytes_with_suffix: 2 new integration tests passedtest_head::63 passed,test_tail::154 passed (12 ignored)head -c 0K,head -c 00K,head -c +0K,tail -c 0K,tail -c 00Kprint nothing;tail -c +0Kprints everything;head -c 0Berrors