Skip to content

head/tail: parse zero byte counts with size suffix as 0 bytes - #13706

Open
MeGaurav4 wants to merge 1 commit into
uutils:mainfrom
MeGaurav4:fix/head-zero-bytes-suffix
Open

head/tail: parse zero byte counts with size suffix as 0 bytes#13706
MeGaurav4 wants to merge 1 commit into
uutils:mainfrom
MeGaurav4:fix/head-zero-bytes-suffix

Conversation

@MeGaurav4

@MeGaurav4 MeGaurav4 commented Aug 2, 2026

Copy link
Copy Markdown
Contributor

head/tail: parse zero byte counts with size suffix as 0 bytes, not 1KiB

head --bytes=0K, tail -c 0K and 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_max strips leading zeros before the size suffix is interpreted, so 0K becomes the bare suffix K. A bare suffix means "1 of that unit" in parse_size (empty numeric part parses as 1), so 0K parsed 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 0B errors, matching GNU, because B is not valid for head's parser).

Affects only head -c/--bytes and tail -c/--bytes (the only users of this parser).

Verification

  • cargo test -p uucore --lib: 94 passed (includes 5 new unit tests in parse_signed_num)
  • cargo test --features "head tail" test_zero_bytes_with_suffix: 2 new integration tests passed
  • Full suites, no regressions: test_head:: 63 passed, test_tail:: 154 passed (12 ignored)
  • Behavior cross-checked against GNU coreutils: head -c 0K, head -c 00K, head -c +0K, tail -c 0K, tail -c 00K print nothing; tail -c +0K prints everything; head -c 0B errors

@MeGaurav4
MeGaurav4 force-pushed the fix/head-zero-bytes-suffix branch from 391d862 to 93ee719 Compare August 2, 2026 06:45
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
MeGaurav4 force-pushed the fix/head-zero-bytes-suffix branch from 93ee719 to c7bde4b Compare August 2, 2026 06:49
@codspeed-hq

codspeed-hq Bot commented Aug 2, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 3.03%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 1 improved benchmark
✅ 344 untouched benchmarks
⏩ 46 skipped benchmarks1

Performance Changes

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)

Open in CodSpeed

Footnotes

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

@github-actions

github-actions Bot commented Aug 2, 2026

Copy link
Copy Markdown

GNU testsuite comparison:

Skip an intermittent issue tests/tail/retry (fails in this run but passes in the 'main' branch)
Skipping an intermittent issue tests/date/date-locale-hour (passes in this run but fails in the 'main' branch)

@RenjiSann

Copy link
Copy Markdown
Collaborator

This change makes tests::test_gnu_compatibility in head.rs fail

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.

head --bytes=0K outputs up to 1 KiB instead of zero bytes

2 participants