misc/118317: Incorrect gzeof() return value in zlib when reading uncompressed files

Gregor Maier gregor at net.t-labs.tu-berlin.de
Wed Nov 28 11:00:07 PST 2007


>Number:         118317
>Category:       misc
>Synopsis:       Incorrect gzeof() return value in zlib when reading uncompressed files
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Nov 28 19:00:06 UTC 2007
>Closed-Date:
>Last-Modified:
>Originator:     Gregor Maier
>Release:        FreeBSD 6 (and 7)
>Organization:
>Environment:
FreeBSD hostname 6.2-RELEASE-p8 FreeBSD 6.2-RELEASE-p8 #0: Tue Oct 16 09:37:43 CEST 2007     root at hostname:/usr/src/sys/i386/compile/HOSTNAME  i386
>Description:
When reading uncompressed files with gzread() the EOF indicator is not
always set correctly. The EOF indicator is only set, when the underlying
fread() returned 0. This is incorrect, since any return value that is
shorter than the nmemb argument may indicate an EOF. The correct
behavior is to explicitly check feof() after the fread() determine
whether EOF occored.

Furthermore the EOF indicator is not set on empty files. 

The attached patch fixes these problems. 
The fix for empty files (first chunk in the patch) was taken from Debian. 
The fix for short byte count on fread (second chunk) is my own. 

>How-To-Repeat:
rc = gzread(zfp, buf, 256);
if (rc < 256) {
	if (gzeof(zfp))
		printf("Had EOF");
	else
		prinf("Not EOF, but short byte count returned");
}
If rc!=0 and rz<256, gzeof() will never indicate and EOF, even if
underlying fread() reported an EOF.
>Fix:
see attached patch

Patch attached with submission follows:

diff -Naur libz.orig/gzio.c libz/gzio.c
--- libz.orig/gzio.c	2007-11-28 19:37:59.000000000 +0100
+++ libz/gzio.c	2007-11-28 19:39:42.000000000 +0100
@@ -302,6 +302,7 @@
         if (len) s->inbuf[0] = s->stream.next_in[0];
         errno = 0;
         len = (uInt)fread(s->inbuf + len, 1, Z_BUFSIZE >> len, s->file);
+        if (len == 0 && feof(s->file)) s->z_eof = 1;
         if (len == 0 && ferror(s->file)) s->z_err = Z_ERRNO;
         s->stream.avail_in += len;
         s->stream.next_in = s->inbuf;
@@ -444,7 +445,7 @@
             len -= s->stream.avail_out;
             s->in  += len;
             s->out += len;
-            if (len == 0) s->z_eof = 1;
+            if (feof(s->file)) s->z_eof = 1;
             return (int)len;
         }
         if (s->stream.avail_in == 0 && !s->z_eof) {


>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list