From owner-svn-src-all@freebsd.org  Sat Mar 14 05:57:23 2020
Return-Path: <owner-svn-src-all@freebsd.org>
Delivered-To: svn-src-all@mailman.nyi.freebsd.org
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1])
 by mailman.nyi.freebsd.org (Postfix) with ESMTP id 4B9DD27820D;
 Sat, 14 Mar 2020 05:57:23 +0000 (UTC)
 (envelope-from delphij@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)
 server-signature RSA-PSS (4096 bits)
 client-signature RSA-PSS (4096 bits) client-digest SHA256)
 (Client CN "mxrelay.nyi.freebsd.org",
 Issuer "Let's Encrypt Authority X3" (verified OK))
 by mx1.freebsd.org (Postfix) with ESMTPS id 48fX03132nz4Q7D;
 Sat, 14 Mar 2020 05:57:23 +0000 (UTC)
 (envelope-from delphij@FreeBSD.org)
Received: from repo.freebsd.org (repo.freebsd.org
 [IPv6:2610:1c1:1:6068::e6a:0])
 (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits))
 (Client did not present a certificate)
 by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 1E9551A5FF;
 Sat, 14 Mar 2020 05:57:23 +0000 (UTC)
 (envelope-from delphij@FreeBSD.org)
Received: from repo.freebsd.org ([127.0.1.37])
 by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 02E5vNVI097088;
 Sat, 14 Mar 2020 05:57:23 GMT (envelope-from delphij@FreeBSD.org)
Received: (from delphij@localhost)
 by repo.freebsd.org (8.15.2/8.15.2/Submit) id 02E5vMBA097086;
 Sat, 14 Mar 2020 05:57:22 GMT (envelope-from delphij@FreeBSD.org)
Message-Id: <202003140557.02E5vMBA097086@repo.freebsd.org>
X-Authentication-Warning: repo.freebsd.org: delphij set sender to
 delphij@FreeBSD.org using -f
From: Xin LI <delphij@FreeBSD.org>
Date: Sat, 14 Mar 2020 05:57:22 +0000 (UTC)
To: src-committers@freebsd.org, svn-src-all@freebsd.org,
 svn-src-head@freebsd.org
Subject: svn commit: r358988 - head/usr.bin/gzip
X-SVN-Group: head
X-SVN-Commit-Author: delphij
X-SVN-Commit-Paths: head/usr.bin/gzip
X-SVN-Commit-Revision: 358988
X-SVN-Commit-Repository: base
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
X-BeenThere: svn-src-all@freebsd.org
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: "SVN commit messages for the entire src tree \(except for &quot;
 user&quot; and &quot; projects&quot; \)" <svn-src-all.freebsd.org>
List-Unsubscribe: <https://lists.freebsd.org/mailman/options/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=unsubscribe>
List-Archive: <http://lists.freebsd.org/pipermail/svn-src-all/>
List-Post: <mailto:svn-src-all@freebsd.org>
List-Help: <mailto:svn-src-all-request@freebsd.org?subject=help>
List-Subscribe: <https://lists.freebsd.org/mailman/listinfo/svn-src-all>,
 <mailto:svn-src-all-request@freebsd.org?subject=subscribe>
X-List-Received-Date: Sat, 14 Mar 2020 05:57:23 -0000

Author: delphij
Date: Sat Mar 14 05:57:22 2020
New Revision: 358988
URL: https://svnweb.freebsd.org/changeset/base/358988

Log:
  Remove unneeded checks for prelen.
  
  In order to determine the type of a compressed file, we have to read
  in the first four bytes which may also be important for decompression
  purposes, to do that we would pass the buffer that we have already
  read in, along with the size of it.
  
  Rename header1 to fourbytes to make that explicit, and remove all
  checks for prelen.
  
  Reported by:	cem
  Reviewed by:	cem
  MFC after:	2 weeks
  Differential Revision:	https://reviews.freebsd.org/D24034

Modified:
  head/usr.bin/gzip/gzip.c
  head/usr.bin/gzip/unlz.c
  head/usr.bin/gzip/unpack.c

Modified: head/usr.bin/gzip/gzip.c
==============================================================================
--- head/usr.bin/gzip/gzip.c	Sat Mar 14 02:36:45 2020	(r358987)
+++ head/usr.bin/gzip/gzip.c	Sat Mar 14 05:57:22 2020	(r358988)
@@ -1443,7 +1443,7 @@ file_uncompress(char *file, char *outfile, size_t outs
 	struct stat isb, osb;
 	off_t size;
 	ssize_t rbytes;
-	unsigned char header1[4];
+	unsigned char fourbytes[4];
 	enum filetype method;
 	int fd, ofd, zfd = -1;
 	int error;
@@ -1477,8 +1477,8 @@ file_uncompress(char *file, char *outfile, size_t outs
 		goto lose;
 	}
 
-	rbytes = read(fd, header1, sizeof header1);
-	if (rbytes != sizeof header1) {
+	rbytes = read(fd, fourbytes, sizeof fourbytes);
+	if (rbytes != sizeof fourbytes) {
 		/* we don't want to fail here. */
 #ifndef SMALL
 		if (fflag)
@@ -1492,7 +1492,7 @@ file_uncompress(char *file, char *outfile, size_t outs
 	}
 	infile_newdata(rbytes);
 
-	method = file_gettype(header1);
+	method = file_gettype(fourbytes);
 #ifndef SMALL
 	if (fflag == 0 && method == FT_UNKNOWN) {
 		maybe_warnx("%s: not in gzip format", file);
@@ -1516,7 +1516,7 @@ file_uncompress(char *file, char *outfile, size_t outs
 		infile_newdata(rv);
 		timestamp = le32dec(&ts[0]);
 
-		if (header1[3] & ORIG_NAME) {
+		if (fourbytes[3] & ORIG_NAME) {
 			rbytes = pread(fd, name, sizeof(name) - 1, GZIP_ORIGNAME);
 			if (rbytes < 0) {
 				maybe_warn("can't read %s", file);
@@ -1818,7 +1818,7 @@ static void
 handle_stdin(void)
 {
 	struct stat isb;
-	unsigned char header1[4];
+	unsigned char fourbytes[4];
 	size_t in_size;
 	off_t usize, gsize;
 	enum filetype method;
@@ -1849,16 +1849,16 @@ handle_stdin(void)
 		goto out;
 	}
 
-	bytes_read = read_retry(STDIN_FILENO, header1, sizeof header1);
+	bytes_read = read_retry(STDIN_FILENO, fourbytes, sizeof fourbytes);
 	if (bytes_read == -1) {
 		maybe_warn("can't read stdin");
 		goto out;
-	} else if (bytes_read != sizeof(header1)) {
+	} else if (bytes_read != sizeof(fourbytes)) {
 		maybe_warnx("(stdin): unexpected end of file");
 		goto out;
 	}
 
-	method = file_gettype(header1);
+	method = file_gettype(fourbytes);
 	switch (method) {
 	default:
 #ifndef SMALL
@@ -1866,17 +1866,17 @@ handle_stdin(void)
 			maybe_warnx("unknown compression format");
 			goto out;
 		}
-		usize = cat_fd(header1, sizeof header1, &gsize, STDIN_FILENO);
+		usize = cat_fd(fourbytes, sizeof fourbytes, &gsize, STDIN_FILENO);
 		break;
 #endif
 	case FT_GZIP:
 		usize = gz_uncompress(STDIN_FILENO, STDOUT_FILENO,
-			      (char *)header1, sizeof header1, &gsize, "(stdin)");
+			      (char *)fourbytes, sizeof fourbytes, &gsize, "(stdin)");
 		break;
 #ifndef NO_BZIP2_SUPPORT
 	case FT_BZIP2:
 		usize = unbzip2(STDIN_FILENO, STDOUT_FILENO,
-				(char *)header1, sizeof header1, &gsize);
+				(char *)fourbytes, sizeof fourbytes, &gsize);
 		break;
 #endif
 #ifndef NO_COMPRESS_SUPPORT
@@ -1886,27 +1886,27 @@ handle_stdin(void)
 			goto out;
 		}
 
-		usize = zuncompress(in, stdout, (char *)header1,
-		    sizeof header1, &gsize);
+		usize = zuncompress(in, stdout, (char *)fourbytes,
+		    sizeof fourbytes, &gsize);
 		fclose(in);
 		break;
 #endif
 #ifndef NO_PACK_SUPPORT
 	case FT_PACK:
 		usize = unpack(STDIN_FILENO, STDOUT_FILENO,
-			       (char *)header1, sizeof header1, &gsize);
+			       (char *)fourbytes, sizeof fourbytes, &gsize);
 		break;
 #endif
 #ifndef NO_XZ_SUPPORT
 	case FT_XZ:
 		usize = unxz(STDIN_FILENO, STDOUT_FILENO,
-			     (char *)header1, sizeof header1, &gsize);
+			     (char *)fourbytes, sizeof fourbytes, &gsize);
 		break;
 #endif
 #ifndef NO_LZ_SUPPORT
 	case FT_LZ:
 		usize = unlz(STDIN_FILENO, STDOUT_FILENO,
-			     (char *)header1, sizeof header1, &gsize);
+			     (char *)fourbytes, sizeof fourbytes, &gsize);
 		break;
 #endif
 	}

Modified: head/usr.bin/gzip/unlz.c
==============================================================================
--- head/usr.bin/gzip/unlz.c	Sat Mar 14 02:36:45 2020	(r358987)
+++ head/usr.bin/gzip/unlz.c	Sat Mar 14 05:57:22 2020	(r358988)
@@ -618,8 +618,6 @@ unlz(int fin, int fout, char *pre, size_t prelen, off_
 
 	char header[HDR_SIZE];
 
-	if (prelen > sizeof(header))
-		return -1;
 	if (pre && prelen)
 		memcpy(header, pre, prelen);
 	

Modified: head/usr.bin/gzip/unpack.c
==============================================================================
--- head/usr.bin/gzip/unpack.c	Sat Mar 14 02:36:45 2020	(r358987)
+++ head/usr.bin/gzip/unpack.c	Sat Mar 14 05:57:22 2020	(r358988)
@@ -156,9 +156,6 @@ unpack_parse_header(int in, int out, char *pre, size_t
 	ssize_t bytesread;		/* Bytes read from the file */
 	int i, j, thisbyte;
 
-	if (prelen > sizeof hdr)
-		maybe_err("prelen too long");
-
 	/* Prepend the header buffer if we already read some data */
 	if (prelen != 0)
 		memcpy(hdr, pre, prelen);