Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 28 Nov 2007 18:49:56 GMT
From:      Gregor Maier <gregor@net.t-labs.tu-berlin.de>
To:        freebsd-gnats-submit@FreeBSD.org
Subject:   misc/118317: Incorrect gzeof() return value in zlib when reading uncompressed files
Message-ID:  <200711281849.lASInu5U082192@www.freebsd.org>
Resent-Message-ID: <200711281900.lASJ065R001211@freefall.freebsd.org>

next in thread | raw e-mail | index | archive | help

>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@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:



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200711281849.lASInu5U082192>