Bug 563618 - cmp -s returns 1 even if files are identical
Summary: cmp -s returns 1 even if files are identical
Keywords:
Status: CLOSED ERRATA
Alias: None
Product: Red Hat Enterprise Linux 5
Classification: Red Hat
Component: diffutils
Version: 5.4
Hardware: All
OS: Linux
high
medium
Target Milestone: rc
: ---
Assignee: Tim Waugh
QA Contact: Martin Kyral
URL:
Whiteboard:
Depends On:
Blocks: 565366 743405 807971
TreeView+ depends on / blocked
 
Reported: 2010-02-10 18:18 UTC by Jeff Bastian
Modified: 2018-11-30 23:09 UTC (History)
4 users (show)

Fixed In Version: diffutils-2.8.1-16.el5
Doc Type: Bug Fix
Doc Text:
When using the cmp command's -s option to compare files, incorrect results would be returned for special files whose metadata is not accurate, such as files in the proc filesystem. This has been corrected by always reading the content of files whose length is reported as zero bytes.
Clone Of:
Environment:
Last Closed: 2013-01-08 07:16:45 UTC
Target Upstream Version:
Embargoed:


Attachments (Terms of Use)
patch to check for procfs (1.66 KB, patch)
2010-02-10 18:18 UTC, Jeff Bastian
no flags Details | Diff
patch to check for 0 byte files (831 bytes, patch)
2010-02-11 15:18 UTC, Jeff Bastian
no flags Details | Diff


Links
System ID Private Priority Status Summary Last Updated
Red Hat Product Errata RHBA-2013:0036 0 normal SHIPPED_LIVE diffutils bug fix update 2013-01-07 15:28:32 UTC

Description Jeff Bastian 2010-02-10 18:18:44 UTC
Created attachment 390077 [details]
patch to check for procfs

Description of problem:
'cmp -s' tries to take a shortcut and compare file sizes before looking at their contents, and assumes if the file sizes are different, it can safely report that the files are different.  This works in most cases, but it can fail with special filesystems like procfs where all files have a size of 0 bytes.

For example, if you copy /proc/scsi/scsi to /tmp and then compare the two, it incorrectly reports that they are different.


Version-Release number of selected component (if applicable):
diffutils-2.8.1-15.2.3.el5.x86_64

How reproducible:
every time

Steps to Reproduce:
1. cp /proc/scsi/scsi /tmp
2. cmp -s /proc/scsi/scsi /tmp/scsi ; echo $?
  
Actual results:
exit status of 1 means the files are different (which is wrong)

Expected results:
exit status of 0

Additional info:
Attached is a patch to skip the shortcut if either of the files being compared are contained within procfs.

Comment 3 Jeff Bastian 2010-02-11 01:16:41 UTC
Also note that cmp without the -s option works fine:

$ cmp /proc/scsi/scsi /tmp/scsi ; echo $?
0
$ cmp -s /proc/scsi/scsi /tmp/scsi ; echo $?
1

Comment 4 Tim Waugh 2010-02-11 10:00:06 UTC
I think it would be preferable to skip the check if either file is length 0.  That takes care of procfs, sysfs, any other sensible on-demand type fs, avoids magic numbers, and adds very little overhead (1 syscall per file) in the case of real zero-length files.

Comment 5 Jeff Bastian 2010-02-11 15:18:23 UTC
Created attachment 390273 [details]
patch to check for 0 byte files

Good point, and the patch is actually much shorter and simpler this way.

Comment 11 RHEL Program Management 2010-08-09 18:32:57 UTC
This request was evaluated by Red Hat Product Management for
inclusion in the current release of Red Hat Enterprise Linux.
Because the affected component is not scheduled to be updated in the
current release, Red Hat is unfortunately unable to address this
request at this time. Red Hat invites you to ask your support
representative to propose this request, if appropriate and relevant,
in the next release of Red Hat Enterprise Linux.

Comment 13 RHEL Program Management 2011-01-11 20:56:04 UTC
This request was evaluated by Red Hat Product Management for
inclusion in the current release of Red Hat Enterprise Linux.
Because the affected component is not scheduled to be updated in the
current release, Red Hat is unfortunately unable to address this
request at this time. Red Hat invites you to ask your support
representative to propose this request, if appropriate and relevant,
in the next release of Red Hat Enterprise Linux.

Comment 14 RHEL Program Management 2011-01-11 22:28:34 UTC
This request was erroneously denied for the current release of
Red Hat Enterprise Linux.  The error has been fixed and this
request has been re-proposed for the current release.

Comment 16 RHEL Program Management 2011-05-31 13:34:39 UTC
This request was evaluated by Red Hat Product Management for
inclusion in the current release of Red Hat Enterprise Linux.
Because the affected component is not scheduled to be updated in the
current release, Red Hat is unfortunately unable to address this
request at this time. Red Hat invites you to ask your support
representative to propose this request, if appropriate and relevant,
in the next release of Red Hat Enterprise Linux.

Comment 21 Tim Waugh 2012-06-15 16:15:21 UTC
    Technical note added. If any revisions are required, please edit the "Technical Notes" field
    accordingly. All revisions will be proofread by the Engineering Content Services team.
    
    New Contents:
When using the cmp command's -s option to compare files, incorrect results would be returned for special files whose metadata is not accurate, such as files in the proc filesystem. This has been corrected by always reading the content of files whose length is reported as zero bytes.

Comment 25 errata-xmlrpc 2013-01-08 07:16:45 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

http://rhn.redhat.com/errata/RHBA-2013-0036.html


Note You need to log in before you can comment on or make changes to this bug.