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)
Oct 5 2024, 7:27 AM
Unknown Object (File)
Oct 5 2024, 7:25 AM
Unknown Object (File)
Oct 5 2024, 5:30 AM
Unknown Object (File)
Oct 3 2024, 9:45 PM
Unknown Object (File)
Oct 3 2024, 1:52 AM
Unknown Object (File)
Oct 1 2024, 12:39 PM
Unknown Object (File)
Sep 22 2024, 2:34 AM
Unknown Object (File)
Sep 7 2024, 11:01 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

Repository
rG FreeBSD src repository
Lint
Lint Not Applicable
Unit
Tests Not Applicable

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.