From nobody@FreeBSD.org  Wed Nov 28 18:50:39 2007
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34])
	by hub.freebsd.org (Postfix) with ESMTP id BCFE616A46B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 18:50:39 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [IPv6:2001:4f8:fff6::21])
	by mx1.freebsd.org (Postfix) with ESMTP id 5221913C50B
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 18:50:39 +0000 (UTC)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.14.2/8.14.2) with ESMTP id lASInvfI082193
	for <freebsd-gnats-submit@FreeBSD.org>; Wed, 28 Nov 2007 18:49:57 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.14.2/8.14.1/Submit) id lASInu5U082192;
	Wed, 28 Nov 2007 18:49:56 GMT
	(envelope-from nobody)
Message-Id: <200711281849.lASInu5U082192@www.freebsd.org>
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: Incorrect gzeof() return value in zlib when reading uncompressed files
X-Send-Pr-Version: www-3.1
X-GNATS-Notify:

>Number:         118317
>Category:       kern
>Synopsis:       [zlib] [patch] Incorrect gzeof() return value in zlib when reading uncompressed files
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    delphij
>State:          patched
>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:  Fri Jun 11 18:14:15 UTC 2010
>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:
State-Changed-From-To: open->feedback 
State-Changed-By: delphij 
State-Changed-When: Fri Mar 12 03:34:24 UTC 2010 
State-Changed-Why:  
Take since I'm planning for a zlib update. 

Dear submitter, did you submitted the patch to zlib-devel? 


Responsible-Changed-From-To: freebsd-bugs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Fri Mar 12 03:34:24 UTC 2010 
Responsible-Changed-Why:  
Take. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=118317 
State-Changed-From-To: feedback->patched 
State-Changed-By: delphij 
State-Changed-When: Fri Jun 11 18:11:43 UTC 2010 
State-Changed-Why:  
Feedback timed out. 

I believe that the latest zlib have addressed the two issues. 

There is no current plan to MFC this specific change yet, but leave 
this as 'patched' state just in case the behavior is desirable for 
-STABLE branches. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=118317 
>Unformatted:
