Page MenuHomeFreeBSD

cmp: fix -s (silent) when used with skip offsets
ClosedPublic

Authored by emaste on Jan 9 2021, 10:02 PM.
Tags
None
Referenced Files
Unknown Object (File)
Mar 19 2024, 3:31 PM
Unknown Object (File)
Mar 19 2024, 2:57 PM
Unknown Object (File)
Mar 19 2024, 2:21 PM
Unknown Object (File)
Mar 19 2024, 1:50 PM
Unknown Object (File)
Jan 29 2024, 12:20 AM
Unknown Object (File)
Jan 25 2024, 7:16 AM
Unknown Object (File)
Dec 20 2023, 4:25 AM
Unknown Object (File)
Dec 1 2023, 3:45 PM
Subscribers
None

Details

Summary

-s causes cmp to print nothing for differing files, for use when only the exit status is of interest.

-z compares the file size first, for regular files, and fails the comparison early if they do not match.

Prior to this change -s implied -z as an optimization, but this is not valid when file offsets are specified. Now, enable the -z optimization for -s only if both skip arguments are not provided / 0.

PR: 252542
Reported by: William Ahern
Sponsored by: The FreeBSD Foundation

Diff Detail

Lint
Lint Skipped
Unit
Tests Skipped

Event Timeline

emaste created this revision.
emaste added a parent revision: D28072: cmp: use C99 bool.

I first switched the flags to C99 bool in D28072

Fixes: 3e6902efc802ab57fc4e9bf798f2d271b152e7f9

-z basically ignores user-specified file offsets:

echo -n '1234567890' > a
echo -n 'abc567890' > b
cmp -z a b 4 3 => a and b differ

I'm not sure if it's really a bug or not (if not some clarification in the man page would be good), but in principle you could retain the optimization if -z instead checks for size1 - skip1 != size2 - skip2.

This revision is now accepted and ready to land.Jan 10 2021, 8:40 PM

I'm not sure if it's really a bug or not

Agreed, the documentation is not quite clear on this although the code matches the interpretation I would take from the man page. It's probably worth changing the optimization as you suggest, and making this explicit in the man page... I might take a look at it later.