From nobody Fri Apr 19 22:34:12 2024 X-Original-To: dev-commits-src-main@mlmmj.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mlmmj.nyi.freebsd.org (Postfix) with ESMTP id 4VLqFJ4lsLz5Hsxy; Fri, 19 Apr 2024 22:34:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4VLqFJ2qfPz4K66; Fri, 19 Apr 2024 22:34:12 +0000 (UTC) (envelope-from git@FreeBSD.org) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713566052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BG7/9GXmvxV/DdojDeRfeLwGvvZ3NdCjmG5N49XFRZ4=; b=u/5f48M7/GY8BxA0BqCfEnVBB0lBrJZLki7XRGN3yLWPII4pMc7r1DN5ix2TnwhVhroeb8 FZD3LyQbvqkkkLE42KcPIwgwrE7e/Bqc4kWRJ2kz7cCVxWlji34xDAbQztEx9wH00SVbLD qc8iV4IUwIj0Je/piajg5ybCACarY9AcoAcagO6fAaTigNpJwD03H64Tq7+zd2gfkKU8yu zjqrbZmNQYDcpOs1gjx18+YPuNhzcKWCak+CC7pmvGJgcByK1fnoiwXmntHm6WM/GWaYAx +sWdKSSzBbsMHGg7raowXzn7bFaz0IpKV+sIzSzfXpGxwmnvKPZ1bXUb9uw5SA== ARC-Seal: i=1; s=dkim; d=freebsd.org; t=1713566052; a=rsa-sha256; cv=none; b=GKFlfaEzwF+sUn4xCTLBdP+2KQOvRGWzy9rsaOTguEe4PqP/s+7wWAyG27I/dt0lXe3qnT PU1pHryNR/Va0krPzvAF341QM28tEG2rdJy4KEKGS8dNVBIIg1PQEUBi2UFcJYuOQ1ozki m5KXM3oS+wPH/lXzrd3iBtPWSCbPMMuWHiWUcbSoAFgNjsXiUks+RXq6bDQSBJFNn6mIdG fAUSVh2aLW3HWALH71VOV5CcLmUXD2unkMjqmVmNzvLYJaJssugzk5paNUJ3HjC0Y7eKxI qoPNswTHfPoI+BCs/aC0SJ/EhS4+L7T0ehJIPSyE9ZzHcKEa5zlS4MnSYnBxWA== ARC-Authentication-Results: i=1; mx1.freebsd.org; none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=freebsd.org; s=dkim; t=1713566052; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=BG7/9GXmvxV/DdojDeRfeLwGvvZ3NdCjmG5N49XFRZ4=; b=rXIXkyGCmrF9o7N40KwDqBIbtm0d4WQrkeNK10FPtscDEogqa7WZxfVOuaMJqxnZKUw+Kg k7cbHhv1ytq90NcsLcnctbSx/eB2zE/nEd9+M3FJfal540sbpHMfOULhq1R1yFuQkKiYZg +3z7PXnm+oAZXQKDpMNxxcYpYPN8pFYPYTRTnrFxpD6Y5IvzP1U5DKzPzovTzBz/OvRap0 naliNoTcBvv03mCtIb0ENfUfWry9dsZjGQIX+mDDe0zGhrh5OAiAah426jGm+TZv/96niY MmYcxJx80xtZ3AmbDRppJpUm74W8Gnej+NAS/dL0bdcbjuHUy949ebCK8AcbcA== Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 4VLqFJ2P9Gz19VG; Fri, 19 Apr 2024 22:34:12 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.17.1/8.17.1) with ESMTP id 43JMYCam092815; Fri, 19 Apr 2024 22:34:12 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.17.1/8.17.1/Submit) id 43JMYCgk092812; Fri, 19 Apr 2024 22:34:12 GMT (envelope-from git) Date: Fri, 19 Apr 2024 22:34:12 GMT Message-Id: <202404192234.43JMYCgk092812@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-main@FreeBSD.org From: Warner Losh Subject: git: 725a9f47324d - main - bsdiff: Use mmap instead of malloc List-Id: Commit messages for the main branch of the src repository List-Archive: https://lists.freebsd.org/archives/dev-commits-src-main List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-BeenThere: dev-commits-src-main@freebsd.org Sender: owner-dev-commits-src-main@FreeBSD.org MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/main X-Git-Reftype: branch X-Git-Commit: 725a9f47324d42037db93c27ceb40d4956872f3e Auto-Submitted: auto-generated The branch main has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=725a9f47324d42037db93c27ceb40d4956872f3e commit 725a9f47324d42037db93c27ceb40d4956872f3e Author: Ricardo Branco AuthorDate: 2024-04-19 22:32:43 +0000 Commit: Warner Losh 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 #include #include +#include #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 #include #include +#include #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); }