Skip to content

hexdump: end offset is the input size when -s skips past the end of files - #670

Merged
sylvestre merged 4 commits into
uutils:mainfrom
jayhemnani9910:hexdump-skip-past-end
Oct 21, 2026
Merged

sylvestre merged 4 commits into
uutils:mainfrom
jayhemnani9910:hexdump-skip-past-end

Conversation

@jayhemnani9910

Copy link
Copy Markdown
Contributor

When -s skips past the end of the named files, hexdump prints the requested skip as the last offset. util-linux 2.41.3 prints the size of the input, and nothing for an empty file:

$ printf abc > f1; printf def > f2; : > empty
$ hexdump -C -s 100 f1 f2     util-linux: 00000006   uutils: 00000064
$ hexdump -s 5 f1             util-linux: 0000003    uutils: 0000005
$ hexdump -s 5 empty          util-linux: (nothing)  uutils: 0000005

skip_bytes now does what util-linux does: a regular file that the skip covers whole is skipped by its size, and the offset is the total size skipped. Stdin, devices and pipes are seeked or read forward, and the offset stays the requested skip, so hexdump -s 5 /dev/null still prints 0000005 and test_hexdump_skip_beyond_file is unchanged.

New tests test_hexdump_skip_beyond_named_files (fails without the change) and test_hexdump_skip_device. The hexdump tests pass (24). Offsets match util-linux for -s from 3 to 2000 with -C, -x and the default format on empty and small files, several files, /dev/null, /dev/zero, a directory followed by a file, and redirected stdin.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 05:34

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/hexdump/src/hexdump.rs Outdated
Ok(file_size) => {
if remaining >= file_size {
remaining -= file_size;
let is_stdin = self.file_paths[self.current_file_index] == "/dev/stdin";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

comparing the path string is a bit fragile, hexdump /dev/stdin < file is treated as stdin here
could the reader remember that no file was given instead?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 2b02bf8: the reader now remembers that no file was given, so a named /dev/stdin is handled like any other named file. util-linux does the same (hexdump -s 100 /dev/stdin < file prints 0000003), and there is a test for it now.

Comment thread src/uu/hexdump/src/hexdump.rs Outdated
self.current_file().seek(SeekFrom::Start(remaining))
} else {
self.current_file()
.seek(SeekFrom::Current(remaining as i64))

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

remaining as i64 wraps for a skip above i64::MAX, could use i64::try_from?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed in 2b02bf8 to i64::try_from. A skip that does not fit is read instead of seeked.


#[test]
#[cfg(target_os = "linux")]
fn test_hexdump_skip_device() {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why only linux? /dev/null and /dev/zero are there on all unix

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it is cfg(unix) now (758a21e).

Copilot AI balanced review requested due to automatic review settings October 21, 2026 07:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

Comment thread src/uu/hexdump/src/hexdump.rs Outdated
Ok(file_size) => {
if remaining >= file_size {
remaining -= file_size;
let reads_stdin = self.reads_stdin;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need this variable? self.reads_stdin in the guard should work

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, it works in the guard. Dropped it in f6d5307.

Copilot AI balanced review requested due to automatic review settings October 21, 2026 13:36

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing. Review the Copilot code review billing settings.

@sylvestre
sylvestre merged commit e24bc80 into uutils:main Oct 21, 2026
14 checks passed
@sylvestre

Copy link
Copy Markdown
Contributor

Thanks for your PR

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.

3 participants