From wb@yorikke.arb-phys.uni-dortmund.de  Tue Aug 25 07:00:24 1998
Received: from yorikke.arb-phys.uni-dortmund.de (yorikke.arb-phys.uni-dortmund.de [192.109.44.12])
          by hub.freebsd.org (8.8.8/8.8.8) with ESMTP id HAA01229
          for <FreeBSD-gnats-submit@freebsd.org>; Tue, 25 Aug 1998 07:00:20 -0700 (PDT)
          (envelope-from wb@yorikke.arb-phys.uni-dortmund.de)
Received: (from wb@localhost)
	by yorikke.arb-phys.uni-dortmund.de (8.9.1/8.9.1) id PAA17757;
	Tue, 25 Aug 1998 15:59:32 +0200 (MET DST)
	(envelope-from wb)
Message-Id: <199808251359.PAA17757@yorikke.arb-phys.uni-dortmund.de>
Date: Tue, 25 Aug 1998 15:59:32 +0200 (MET DST)
From: "Wilhelm B. Kloke" <wb@yorikke.arb-phys.uni-dortmund.de>
Reply-To: wb@yorikke.arb-phys.uni-dortmund.de
To: FreeBSD-gnats-submit@freebsd.org
Subject: fclose(3) dumps core on NULL
X-Send-Pr-Version: 3.2

>Number:         7742
>Category:       bin
>Synopsis:       fclose(3) dumps core on NULL
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Tue Aug 25 07:10:01 PDT 1998
>Closed-Date:    Wed Sep 9 17:24:02 MEST 1998
>Last-Modified:  Mon Sep 28 06:10:01 PDT 1998
>Originator:     Wilhelm B. Kloke
>Release:        FreeBSD 3.0-CURRENT i386
>Organization:
>Environment:

	FreeBSD 3.0-CURRENT i386

>Description:

	I found for the 2nd time that installing a program failed
	for fclose coredumping when ivoked with NULL. (teTeX-0.9)

>How-To-Repeat:

	Easily.

>Fix:
	
	The man page doe not say anything on this. But it seems
	programming practice, closing a file after open, even if
	the open failed.
	So the library fclose should return gracefully when
	invoked with NULL.

>Release-Note:
>Audit-Trail:

From: Martin Cracauer <cracauer@cons.org>
To: wb@yorikke.arb-phys.uni-dortmund.de, FreeBSD-gnats-submit@FreeBSD.ORG
Cc:  Subject: Re: bin/7742: fclose(3) dumps core on NULL
Date: Mon, 7 Sep 1998 16:47:38 +0200

 --6TrnltStXW4iwmi0
 Content-Type: text/plain; charset=us-ascii
 
 In <199808251359.PAA17757@yorikke.arb-phys.uni-dortmund.de>, Wilhelm B. Kloke wrote: 
 > 	I found for the 2nd time that installing a program failed
 > 	for fclose coredumping when ivoked with NULL. (teTeX-0.9)
 
 > 	The man page doe not say anything on this. But it seems
 > 	programming practice, closing a file after open, even if
 > 	the open failed.
 > 	So the library fclose should return gracefully when
 > 	invoked with NULL.
 
 Urgs. I understand people want to close "just in case some resources
 are left over" from the failed open.
 
 Sadly, this is complete nonsense, since the FILE pointer doesn't point
 to anything that resembles a pointer to anything to clean up if it is
 NULL and fopen doesn't return anything else than NULL on errors.
 
 I checked that SunOS, Solaris and Linux actually support this, and I
 think we can do so as well, since:
 - The cost is near zero, since the pointer we check is followed
   anyway. 
 - There is a way to report the error, a careful programmer (checking
   the return value of fclose) will be notified on the error
   nothingtheless. 
 - The probability that the NULL pointer is carried around further by the
   caller is low.
 
 I could commit the fix as follows, but could someone with more
 authority approve it, please? I don't want to be eaten alive :-)
 
 Martin
 -- 
 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 Martin Cracauer <cracauer@cons.org> http://www.cons.org/cracauer/
 BSD User Group Hamburg/Germany      http://www.bsdhh.org/
 
 --6TrnltStXW4iwmi0
 Content-Type: text/plain; charset=us-ascii
 Content-Disposition: attachment; filename=diff
 
 Index: fclose.c
 ===================================================================
 RCS file: /home/ncvs/src/lib/libc/stdio/fclose.c,v
 retrieving revision 1.6
 diff -c -r1.6 fclose.c
 *** fclose.c	1998/04/11 07:40:42	1.6
 --- fclose.c	1998/09/07 14:14:21
 ***************
 *** 54,60 ****
   {
   	register int r;
   
 ! 	if (fp->_flags == 0) {	/* not open! */
   		errno = EBADF;
   		return (EOF);
   	}
 --- 54,60 ----
   {
   	register int r;
   
 ! 	if (fp == NULL || fp->_flags == 0) {	/* not open! */
   		errno = EBADF;
   		return (EOF);
   	}
 
 --6TrnltStXW4iwmi0--
State-Changed-From-To: open->closed 
State-Changed-By: cracauer 
State-Changed-When: Wed Sep 9 17:24:02 MEST 1998 
State-Changed-Why:  
Change request rejected. 

The error people are probably fall into is that they try to pass the 
handle they got from fopen to fclose "just in case there is some 
cleanup to make on this object". This is nonsense, since it will be a 
NULL pointer and does not point to any object at all. 

Insofar the manpage is right as well. 

From: "Wilhelm B. Kloke" <wb@yorikke.arb-phys.uni-dortmund.de>
To: freebsd-gnats-submit@freebsd.org
Cc:  Subject: Re: bin/7742: fclose(3) dumps core on NULL
Date: Mon, 28 Sep 1998 15:03:20 +0200 (MET DST)

 > Change request rejected. 
 
 Just let me comment thus.
 
 Why make running programs (on some other systems) invalid on so little reason?
 Just letting such programs coredump is just bad marketing.
 
 The whole BSD marketing is faulty, anyway. The schisma between Net, Free
 and Open is only understandable for people who know the history
 of the maoist KPD-ML parties in Germany (in the seventies), or the medieval
 schismata in the christianity. FreeBSD is going to be the 4th international
 of programming.
>Unformatted:
