From nobody@FreeBSD.org  Tue Dec  5 05:28:27 2006
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [69.147.83.52])
	by hub.freebsd.org (Postfix) with ESMTP id 62AE616A40F
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  5 Dec 2006 05:28:27 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [69.147.83.33])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 1874843CA3
	for <freebsd-gnats-submit@FreeBSD.org>; Tue,  5 Dec 2006 05:27:50 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id kB55SQEu020757
	for <freebsd-gnats-submit@FreeBSD.org>; Tue, 5 Dec 2006 05:28:26 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id kB55SQt7020756;
	Tue, 5 Dec 2006 05:28:26 GMT
	(envelope-from nobody)
Message-Id: <200612050528.kB55SQt7020756@www.freebsd.org>
Date: Tue, 5 Dec 2006 05:28:26 GMT
From: anatoly<techtonik@php.net>
To: freebsd-gnats-submit@FreeBSD.org
Subject: [patch] bsdiff - explicitly open files in binary mode
X-Send-Pr-Version: www-3.0

>Number:         106358
>Category:       bin
>Synopsis:       [patch] bsdiff(1) - explicitly open files in binary mode
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    cperciva
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Dec 05 05:30:07 GMT 2006
>Closed-Date:    Tue Dec 05 20:24:08 GMT 2006
>Last-Modified:  Tue Dec  5 20:30:12 GMT 2006
>Originator:     anatoly
>Release:        6.2-RC1
>Organization:
BSUIR
>Environment:
>Description:
bsdiff relies on default behavior of underlying operating system to work with files. It assumes that files are opened in binary mode by default. This might not be true for some platforms. For example, the problem exists on windows platform.

This patch explicitly opens files in binary mode.
>How-To-Repeat:

>Fix:


Patch attached with submission follows:

diff -u3 -r src_old\usr.bin\bsdiff\bsdiff\bsdiff.c src\usr.bin\bsdiff\bsdiff\bsdiff.c
--- src_old\usr.bin\bsdiff\bsdiff\bsdiff.c	Fri Dec 01 07:45:24 2006
+++ src\usr.bin\bsdiff\bsdiff\bsdiff.c	Tue Dec 05 07:16:48 2006
@@ -216,7 +216,7 @@
 
 	/* Allocate oldsize+1 bytes instead of oldsize bytes to ensure
 		that we never try to malloc(0) and get a NULL pointer */
-	if(((fd=open(argv[1],O_RDONLY,0))<0) ||
+	if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
 		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
 		((old=malloc(oldsize+1))==NULL) ||
 		(lseek(fd,0,SEEK_SET)!=0) ||
@@ -232,7 +232,7 @@
 
 	/* Allocate newsize+1 bytes instead of newsize bytes to ensure
 		that we never try to malloc(0) and get a NULL pointer */
-	if(((fd=open(argv[2],O_RDONLY,0))<0) ||
+	if(((fd=open(argv[2],O_RDONLY|O_BINARY,0))<0) ||
 		((newsize=lseek(fd,0,SEEK_END))==-1) ||
 		((new=malloc(newsize+1))==NULL) ||
 		(lseek(fd,0,SEEK_SET)!=0) ||
@@ -245,7 +245,7 @@
 	eblen=0;
 
 	/* Create the patch file */
-	if ((pf = fopen(argv[3], "w")) == NULL)
+	if ((pf = fopen(argv[3], "wb")) == NULL)
 		err(1, "%s", argv[3]);
 
 	/* Header is
diff -u3 -r src_old\usr.bin\bsdiff\bspatch\bspatch.c src\usr.bin\bsdiff\bspatch\bspatch.c
--- src_old\usr.bin\bsdiff\bspatch\bspatch.c	Tue Dec 05 07:21:14 2006
+++ src\usr.bin\bsdiff\bspatch\bspatch.c	Tue Dec 05 07:24:12 2006
@@ -71,7 +71,7 @@
 	if(argc!=4) errx(1,"usage: %s oldfile newfile patchfile\n",argv[0]);
 
 	/* Open patch file */
-	if ((f = fopen(argv[3], "r")) == NULL)
+	if ((f = fopen(argv[3], "rb")) == NULL)
 		err(1, "fopen(%s)", argv[3]);
 
 	/*
@@ -109,21 +109,21 @@
 	/* Close patch file and re-open it via libbzip2 at the right places */
 	if (fclose(f))
 		err(1, "fclose(%s)", argv[3]);
-	if ((cpf = fopen(argv[3], "r")) == NULL)
+	if ((cpf = fopen(argv[3], "rb")) == NULL)
 		err(1, "fopen(%s)", argv[3]);
 	if (fseeko(cpf, 32, SEEK_SET))
 		err(1, "fseeko(%s, %lld)", argv[3],
 		    (long long)32);
 	if ((cpfbz2 = BZ2_bzReadOpen(&cbz2err, cpf, 0, 0, NULL, 0)) == NULL)
 		errx(1, "BZ2_bzReadOpen, bz2err = %d", cbz2err);
-	if ((dpf = fopen(argv[3], "r")) == NULL)
+	if ((dpf = fopen(argv[3], "rb")) == NULL)
 		err(1, "fopen(%s)", argv[3]);
 	if (fseeko(dpf, 32 + bzctrllen, SEEK_SET))
 		err(1, "fseeko(%s, %lld)", argv[3],
 		    (long long)(32 + bzctrllen));
 	if ((dpfbz2 = BZ2_bzReadOpen(&dbz2err, dpf, 0, 0, NULL, 0)) == NULL)
 		errx(1, "BZ2_bzReadOpen, bz2err = %d", dbz2err);
-	if ((epf = fopen(argv[3], "r")) == NULL)
+	if ((epf = fopen(argv[3], "rb")) == NULL)
 		err(1, "fopen(%s)", argv[3]);
 	if (fseeko(epf, 32 + bzctrllen + bzdatalen, SEEK_SET))
 		err(1, "fseeko(%s, %lld)", argv[3],
@@ -131,7 +131,7 @@
 	if ((epfbz2 = BZ2_bzReadOpen(&ebz2err, epf, 0, 0, NULL, 0)) == NULL)
 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
 
-	if(((fd=open(argv[1],O_RDONLY,0))<0) ||
+	if(((fd=open(argv[1],O_RDONLY|O_BINARY,0))<0) ||
 		((oldsize=lseek(fd,0,SEEK_END))==-1) ||
 		((old=malloc(oldsize+1))==NULL) ||
 		(lseek(fd,0,SEEK_SET)!=0) ||
@@ -192,7 +192,7 @@
 		err(1, "fclose(%s)", argv[3]);
 
 	/* Write the new file */
-	if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY,0666))<0) ||
+	if(((fd=open(argv[2],O_CREAT|O_TRUNC|O_WRONLY|O_BINARY,0666))<0) ||
 		(write(fd,new,newsize)!=newsize) || (close(fd)==-1))
 		err(1,"%s",argv[2]);
 

>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->cperciva 
Responsible-Changed-By: remko 
Responsible-Changed-When: Tue Dec 5 06:42:52 UTC 2006 
Responsible-Changed-Why:  
Assign to maintainer. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=106358 
State-Changed-From-To: open->closed 
State-Changed-By: cperciva 
State-Changed-When: Tue Dec 5 20:22:48 UTC 2006 
State-Changed-Why:  
I've committed this patch, with the minor correction of defining O_BINARY 
if it isn't already defined.  (O_BINARY does not exist on POSIX operating 
systems.) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=106358 

From: dfilter@FreeBSD.ORG (dfilter service)
To: bug-followup@FreeBSD.org
Cc:  
Subject: Re: bin/106358: commit references a PR
Date: Tue,  5 Dec 2006 20:22:38 +0000 (UTC)

 cperciva    2006-12-05 20:22:14 UTC
 
   FreeBSD src repository
 
   Modified files:
     usr.bin/bsdiff/bsdiff bsdiff.c 
     usr.bin/bsdiff/bspatch bspatch.c 
   Log:
   Portability fix for non-POSIX operating systems: Open files in binary mode.
   
   PR:             bin/106358
   Submitted by:   techtonik at php dot net
   
   Revision  Changes    Path
   1.2       +7 -3      src/usr.bin/bsdiff/bsdiff/bsdiff.c
   1.2       +10 -6     src/usr.bin/bsdiff/bspatch/bspatch.c
 _______________________________________________
 cvs-all@freebsd.org mailing list
 http://lists.freebsd.org/mailman/listinfo/cvs-all
 To unsubscribe, send any mail to "cvs-all-unsubscribe@freebsd.org"
 
>Unformatted:
