head, tail: keep leading zeros in an invalid count message - #14249
Open
arbelonson-source wants to merge 1 commit into
Open
head, tail: keep leading zeros in an invalid count message#14249arbelonson-source wants to merge 1 commit into
arbelonson-source wants to merge 1 commit into
Conversation
Closes uutils#14229. `parse_signed_num_max` strips leading zeros so the count is read as decimal rather than octal, then hands the trimmed string to the size parser. The error carries that trimmed string, so the zeros the user typed never reach the message: $ tail -c0fb a tail: invalid number of bytes: 'fb' # GNU: '0fb' $ head -n0x a head: invalid number of lines: 'x' # GNU: '0x' Rebuild the error around the untrimmed operand instead. Only the two variants that carry the operand alone are rebuilt; `SizeTooBig` also carries an explanation and `PhysicalMem` is not about the operand, and neither can arise here anyway, since `parse_size_u64_max` clamps rather than overflowing. The sign stays stripped, matching GNU, which reports the operand of `tail -c-0fb` as '0fb'. This also lines the message up with `number_offset`, which already counts leading zeros as part of the number when placing the caret.
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 #14229.
parse_signed_num_maxstrips leading zeros so the count is read as decimal rather than octal, then hands the trimmed string to the size parser. The error carries that trimmed string, so the zeros the user typed never reach the message.tail -c0fb ainvalid number of bytes: '0fb'invalid number of bytes: 'fb'tail -c00x ainvalid number of bytes: '00x'invalid number of bytes: 'x'head -n0x ainvalid number of lines: '0x'invalid number of lines: 'x'tail -c-0fb ainvalid number of bytes: '0fb'invalid number of bytes: 'fb'The error is now rebuilt around the untrimmed operand. Only the two variants that carry the operand and nothing else are rebuilt —
SizeTooBigalso carries an explanation after it andPhysicalMemis not about the operand at all. Neither can arise on this path anyway, sinceparse_size_u64_maxclamps rather than overflowing.The sign stays stripped, matching GNU, which reports the operand of
tail -c-0fbas'0fb'.Worth noting: this also lines the message up with
number_offset, which already counts leading zeros as part of the number when placing the caret — its existing test says "The parser reads leading zeros as part of the number, so they stay", which was not true of the message until now.Testing
Two unit tests on the parser and one integration test each for
headandtail. I verified they catch the bug by reverting the twomap_errcalls alone — the parser tests fail, then pass again once restored.cargo test -p uucore --features parser: 195 passed, 0 failedcargo test --features "head,tail" --no-default-features: 222 passed, 0 failed (220 pre-existing, 2 new)cargo fmt --checkandcargo clippy -p uucore -p uu_head -p uu_tail --all-targets: cleanhead/tailcount arguments against GNU coreutils 9.11: all 34 now match, including the valid cases (-c007,-n07,-c1K) as regression coverOne thing I found but did not fix
While sweeping,
tail -c007zandhead -n0000qsucceed where GNU rejects them — uutils accepts lowercase unit suffixes that GNU does not. That is the suffix question in #13136 rather than this one, so I left it alone and deliberately kept those spellings out of the tests here so the two do not become entangled.Disclosure
Prepared with AI assistance (Claude Code), per the AI policy in CONTRIBUTING.md. On the GPL point raised there: the expected behavior was established by running the installed GNU binaries as a black box and recording their output. I did not read GNU coreutils source while writing this. All testing above was run locally.