git: 725a9f47324d - main - bsdiff: Use mmap instead of malloc

From: Warner Losh <imp_at_FreeBSD.org>
Date: Fri, 19 Apr 2024 22:34:12 UTC
The branch main has been updated by imp:

URL: https://cgit.FreeBSD.org/src/commit/?id=725a9f47324d42037db93c27ceb40d4956872f3e

commit 725a9f47324d42037db93c27ceb40d4956872f3e
Author:     Ricardo Branco <rbranco@suse.de>
AuthorDate: 2024-04-19 22:32:43 +0000
Commit:     Warner Losh <imp@FreeBSD.org>
CommitDate: 2024-04-19 22:33:07 +0000

    bsdiff: Use mmap instead of malloc
    
    Note: This follows the current style of the bsdiff.c and bspatch.c
    files, which is rather far from style(9).
    
    Reviewed by: imp, cpervica
    Pull Request: https://github.com/freebsd/freebsd-src/pull/1076
---
 usr.bin/bsdiff/bsdiff/bsdiff.c   | 25 ++++++++++++-------------
 usr.bin/bsdiff/bspatch/bspatch.c | 16 +++++++++-------
 2 files changed, 21 insertions(+), 20 deletions(-)

diff --git a/usr.bin/bsdiff/bsdiff/bsdiff.c b/usr.bin/bsdiff/bsdiff/bsdiff.c
index a4813253b23e..06a8812308c8 100644
--- a/usr.bin/bsdiff/bsdiff/bsdiff.c
+++ b/usr.bin/bsdiff/bsdiff/bsdiff.c
@@ -38,6 +38,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/mman.h>
 
 #ifndef O_BINARY
 #define O_BINARY 0
@@ -115,7 +116,7 @@ int main(int argc,char *argv[])
 {
 	int fd;
 	u_char *old,*new;
-	off_t oldsize,newsize;
+	off_t oldsize,newsize,xnewsize;
 	saidx_t *I;
 	off_t scan,pos,len;
 	off_t lastscan,lastpos,lastoffset;
@@ -147,10 +148,9 @@ int main(int argc,char *argv[])
 		err(1, "%s", argv[1]);
 	}
 
-	if (((old=malloc(oldsize+1))==NULL) ||
-		(lseek(fd,0,SEEK_SET)!=0) ||
-		(read(fd,old,oldsize)!=oldsize) ||
-		(close(fd)==-1)) err(1,"%s",argv[1]);
+	old = mmap(NULL, oldsize+1, PROT_READ, MAP_SHARED, fd, 0);
+	if (old == MAP_FAILED || close(fd) == -1)
+		err(1, "%s", argv[1]);
 
 	if(((I=malloc((oldsize+1)*sizeof(saidx_t)))==NULL)) err(1,NULL);
 
@@ -168,10 +168,9 @@ int main(int argc,char *argv[])
 		err(1, "%s", argv[2]);
 	}
 
-	if (((new=malloc(newsize+1))==NULL) ||
-		(lseek(fd,0,SEEK_SET)!=0) ||
-		(read(fd,new,newsize)!=newsize) ||
-		(close(fd)==-1)) err(1,"%s",argv[2]);
+	new = mmap(NULL, newsize+1, PROT_READ, MAP_SHARED, fd, 0);
+	if (new == MAP_FAILED || close(fd) == -1)
+		err(1, "%s", argv[2]);
 
 	if(((db=malloc(newsize+1))==NULL) ||
 		((eb=malloc(newsize+1))==NULL)) err(1,NULL);
@@ -304,9 +303,9 @@ int main(int argc,char *argv[])
 		errx(1, "BZ2_bzWriteClose, bz2err = %d", bz2err);
 
 	/* Compute size of compressed diff data */
-	if ((newsize = ftello(pf)) == -1)
+	if ((xnewsize = ftello(pf)) == -1)
 		err(1, "ftello");
-	offtout(newsize - len, header + 16);
+	offtout(xnewsize - len, header + 16);
 
 	/* Write compressed extra data */
 	if ((pfbz2 = BZ2_bzWriteOpen(&bz2err, pf, 9, 0, 0)) == NULL)
@@ -330,8 +329,8 @@ int main(int argc,char *argv[])
 	free(db);
 	free(eb);
 	free(I);
-	free(old);
-	free(new);
+	munmap(old, oldsize+1);
+	munmap(new, newsize+1);
 
 	return 0;
 }
diff --git a/usr.bin/bsdiff/bspatch/bspatch.c b/usr.bin/bsdiff/bspatch/bspatch.c
index d7fabddabbfe..ea43d78b12a1 100644
--- a/usr.bin/bsdiff/bspatch/bspatch.c
+++ b/usr.bin/bsdiff/bspatch/bspatch.c
@@ -42,6 +42,7 @@
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <sys/mman.h>
 
 #ifndef O_BINARY
 #define O_BINARY 0
@@ -151,7 +152,7 @@ int main(int argc, char *argv[])
 	if (cap_enter() < 0)
 		err(1, "failed to enter security sandbox");
 
-	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK);
+	cap_rights_init(&rights_ro, CAP_READ, CAP_FSTAT, CAP_SEEK, CAP_MMAP_R);
 	cap_rights_init(&rights_wr, CAP_WRITE);
 	cap_rights_init(&rights_dir, CAP_UNLINKAT);
 
@@ -220,12 +221,13 @@ int main(int argc, char *argv[])
 		errx(1, "BZ2_bzReadOpen, bz2err = %d", ebz2err);
 
 	if ((oldsize = lseek(oldfd, 0, SEEK_END)) == -1 ||
-	    oldsize > SSIZE_MAX ||
-	    (old = malloc(oldsize)) == NULL ||
-	    lseek(oldfd, 0, SEEK_SET) != 0 ||
-	    read(oldfd, old, oldsize) != oldsize ||
-	    close(oldfd) == -1)
+	    oldsize > SSIZE_MAX)
 		err(1, "%s", argv[1]);
+
+	old = mmap(NULL, oldsize+1, PROT_READ, MAP_SHARED, oldfd, 0);
+	if (old == MAP_FAILED || close(oldfd) != 0)
+		err(1, "%s", argv[1]);
+
 	if ((new = malloc(newsize)) == NULL)
 		err(1, NULL);
 
@@ -294,7 +296,7 @@ int main(int argc, char *argv[])
 	newfile = NULL;
 
 	free(new);
-	free(old);
+	munmap(old, oldsize+1);
 
 	return (0);
 }