From jeff@katya.snet.net  Thu Jun 12 14:55:03 1997
Received: from katya.snet.net (hrfr03-sh2-port80.snet.net [204.60.8.80])
          by hub.freebsd.org (8.8.5/8.8.5) with ESMTP id OAA23016
          for <FreeBSD-gnats-submit@freebsd.org>; Thu, 12 Jun 1997 14:54:41 -0700 (PDT)
Received: (from jeff@localhost) by katya.snet.net (8.8.5/8.7.3) id RAA00636; Thu, 12 Jun 1997 17:53:15 -0400 (EDT)
Message-Id: <199706122153.RAA00636@katya.snet.net>
Date: Thu, 12 Jun 1997 17:53:15 -0400 (EDT)
From: metcalf@snet.net
Reply-To: metcalf@snet.net
To: FreeBSD-gnats-submit@freebsd.org
Subject: /usr/bin/cmp fails with nonzero byte offsets
X-Send-Pr-Version: 3.2

>Number:         3855
>Category:       bin
>Synopsis:       /usr/bin/cmp fails with nonzero byte offsets
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:
>Keywords:
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Thu Jun 12 15:00:01 PDT 1997
>Closed-Date:    Tue Aug 19 07:37:59 PDT 1997
>Last-Modified:  Tue Aug 19 07:38:19 PDT 1997
>Originator:     Jeffrey M. Metcalf
>Release:        FreeBSD 2.2.1-RELEASE i386
>Organization:
Jeffrey M. Metcalf
>Environment:
System: FreeBSD 2.2.1-RELEASE
Architecture: Intel i386

>Description:
	This is a followup to a previous send-pr, identification
	bin/3850.  It provides a revised patch similar to the 
	source code in /usr/bin/cmp in the FreeBSD-3.0 SNAP release.
	The base conversion problem is still an issue in the 3.0 code.

        /usr/bin/cmp fails to perform according to the manual page
	cmp.1 in that nonzero values of skip1 and skip2 for the
	byte offsets results in an 'Invalid argument' error.
	This seems to be due to the fact that the mmap system
	call fails for non page aligned offsets.  Also, only
	base 10 conversion is allowed whereas the manual page 
	implies that octal and hexadecimal byte offsets can be
	entered.


>How-To-Repeat:
	prompt% /usr/bin/cmp file1 file2 skip1 skip2

	where skip1 and skip2 are nonzero byte offsets
	into file1 and file2 respectively.


>Fix:
	Apply the following patch to the sources in the 
	/usr/src/usr.bin/cmp directory:


*** /usr/src/usr.bin/cmp/cmp.c	Thu Jun 12 13:03:06 1997
--- cmp.c	Thu Jun 12 13:18:40 1997
***************
*** 30,33 ****
--- 30,43 ----
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
+  *
+  * Update:
+  *
+  * Author: Jeffrey M. Metcalf
+  * Date  : 12-Jun-97
+  * Re    : Fix bug in 2.2-SOURCES in which cmp cannot deal with nonzero
+  *	   skip byte offsets due to mmap's inability to map non page
+  *	   aligned offsets.  Also, cmp is supposed to allow byte offsets
+  *	   to be entered in decimal, hex, and octal according to cmp.1.
+  *	   Portions of 3.0-SOURCES used.
   */
  
***************
*** 121,126 ****
  	}
  
! 	skip1 = argc > 2 ? strtol(argv[2], NULL, 10) : 0;
! 	skip2 = argc == 4 ? strtol(argv[3], NULL, 10) : 0;
  
  	if (!special) {
--- 131,141 ----
  	}
  
! /*
!  * Use 0 as the base below to include hex and octal conversions.
!  * JM, 12-JUN-97
!  */
! 
! 	skip1 = argc > 2 ? strtol(argv[2], NULL, 0) : 0;
! 	skip2 = argc == 4 ? strtol(argv[3], NULL, 0) : 0;
  
  	if (!special) {
*** /usr/src/usr.bin/cmp/regular.c	Thu Jun 12 13:03:06 1997
--- regular.c	Thu Jun 12 13:21:19 1997
***************
*** 30,33 ****
--- 30,43 ----
   * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   * SUCH DAMAGE.
+  *
+  * Update:
+  *
+  * Author: Jeffrey M. Metcalf
+  * Date  : 12-Jun-97
+  * Re    : Fix bug in 2.2-SOURCES in which cmp cannot deal with nonzero
+  *	   skip byte offsets due to mmap's inability to map non page
+  *	   aligned offsets.  Also, cmp is supposed to allow byte offsets
+  *	   to be entered in decimal, hex, and octal according to cmp.1.
+  *	   Portions of 3.0-SOURCES used.
   */
  
***************
*** 72,75 ****
--- 82,89 ----
  	len2 -= skip2;
  
+ 	pagemask = (off_t)getpagesize() - 1;
+ 	off1 = ROUNDPAGE(skip1);
+ 	off2 = ROUNDPAGE(skip2);
+ 
  	length = MIN(len1, len2);
  	if (length > SIZE_T_MAX)
***************
*** 77,87 ****
  
  	if ((p1 = (u_char *)mmap(NULL,
! 	    (size_t)length, PROT_READ, 0, fd1, skip1)) == (u_char *)-1)
  		err(ERR_EXIT, "%s", file1);
  	if ((p2 = (u_char *)mmap(NULL,
! 	    (size_t)length, PROT_READ, 0, fd2, skip2)) == (u_char *)-1)
  		err(ERR_EXIT, "%s", file2);
  
  	dfound = 0;
  	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
  		if ((ch = *p1) != *p2)
--- 91,103 ----
  
  	if ((p1 = (u_char *)mmap(NULL,
! 	    (size_t)length, PROT_READ, 0, fd1, off1)) == (u_char *)-1)
  		err(ERR_EXIT, "%s", file1);
  	if ((p2 = (u_char *)mmap(NULL,
! 	    (size_t)length, PROT_READ, 0, fd2, off2)) == (u_char *)-1)
  		err(ERR_EXIT, "%s", file2);
  
  	dfound = 0;
+ 	p1 += skip1 - off1;
+ 	p2 += skip2 - off2;
  	for (byte = line = 1; length--; ++p1, ++p2, ++byte) {
  		if ((ch = *p1) != *p2)
>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->closed 
State-Changed-By: jlemon 
State-Changed-When: Tue Aug 19 07:37:59 PDT 1997 
State-Changed-Why:  

See PR 3850. 
>Unformatted:
