Subject: bin/26242: gzip: file_uncompress() error handling problems
To: None <gnats-bugs@gnats.netbsd.org>
From: None <yamt@mwd.biglobe.ne.jp>
List: netbsd-bugs
Date: 07/11/2004 18:39:57
>Number:         26242
>Category:       bin
>Synopsis:       gzip: file_uncompress() error handling problems
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    bin-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Jul 11 09:41:00 UTC 2004
>Closed-Date:
>Last-Modified:
>Originator:     YAMAMOTO Takashi <yamt@mwd.biglobe.ne.jp>
>Release:        NetBSD 2.0F
>Organization:

>Environment:
	
	
System: NetBSD kaeru 2.0F NetBSD 2.0F (build.kaeru) #1518: Thu Jul 8 16:09:03 JST 2004 takashi@kaeru:/home/takashi/work/kernel/build.kaeru i386
Architecture: i386
Machine: i386
>Description:
	- warn() shouldn't be used for EOF.
		% rm a.gz;touch a.gz
		% gzip -d a.gz
		gzip: can't read a.gz: Undefined error: 0
	- as rbytes is of size_t, (rbytes < 0) is always false.
>How-To-Repeat:
>Fix:

Index: gzip.c
===================================================================
RCS file: /cvsroot/src/usr.bin/gzip/gzip.c,v
retrieving revision 1.55
diff -u -p -r1.55 gzip.c
--- gzip.c	11 Jul 2004 07:01:03 -0000	1.55
+++ gzip.c	11 Jul 2004 09:30:24 -0000
@@ -1071,6 +1071,7 @@ file_uncompress(char *file, char *outfil
 	struct stat isb, osb;
 	char *s;
 	off_t size;
+	ssize_t rbytes;
 	ssize_t len = strlen(file);
 	unsigned char header1[4], name[PATH_MAX + 1];
 	enum filetype method;
@@ -1086,13 +1087,17 @@ file_uncompress(char *file, char *outfil
 		maybe_warn("can't open %s", file);
 		goto lose;
 	}
-	if (read(fd, header1, sizeof header1) != sizeof header1) {
+	rbytes = read(fd, header1, sizeof header1);
+	if (rbytes != sizeof header1) {
 		/* we don't want to fail here. */
 #ifndef SMALL
 		if (fflag)
 			goto lose_close_it;
 #endif
-		maybe_warn("can't read %s", file);
+		if (rbytes == -1)
+			maybe_warn("can't read %s", file);
+		else
+			maybe_warnx("%s: unexpected end of file", file);
 		goto lose;
 	}
 
@@ -1154,7 +1159,6 @@ file_uncompress(char *file, char *outfil
 #endif
 
 		if (header1[3] & ORIG_NAME) {
-			size_t rbytes;
 			int i;
 
 			if (lseek(fd, GZIP_ORIGNAME, SEEK_SET) == -1) {
>Release-Note:
>Audit-Trail:
>Unformatted: