changeset 6747:bd5c81f4c585

Call fclose() in all cases, even in the failure case.
author Bruno Haible <bruno@clisp.org>
date Mon, 24 Apr 2006 11:32:00 +0000
parents 6fd163815548
children 427cd89c6d7b
files lib/ChangeLog lib/fwriteerror.c
diffstat 2 files changed, 19 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lib/ChangeLog
+++ b/lib/ChangeLog
@@ -1,3 +1,9 @@
+2006-04-23  Bruno Haible  <bruno@clisp.org>
+
+	* fwriteerror.c (fwriteerror): Call fclose also when an error
+	condition was already detected.
+	Reported by Ben Pfaff <blp@cs.stanford.edu>.
+
 2006-04-19  Derek Price  <derek@ximbiot.com>
 	    Eric Blake  <ebb9@byu.net>
 
--- a/lib/fwriteerror.c
+++ b/lib/fwriteerror.c
@@ -1,5 +1,5 @@
 /* Detect write error on a stream.
-   Copyright (C) 2003-2005 Free Software Foundation, Inc.
+   Copyright (C) 2003-2006 Free Software Foundation, Inc.
    Written by Bruno Haible <bruno@clisp.org>, 2003.
 
    This program is free software; you can redistribute it and/or modify
@@ -47,18 +47,26 @@
   if (ferror (fp))
     {
       if (fflush (fp))
-	return -1; /* errno is set here */
+	goto close_preserving_errno; /* errno is set here */
       /* The stream had an error earlier, but its errno was lost.  If the
 	 error was not temporary, we can get the same errno by writing and
 	 flushing one more byte.  We can do so because at this point the
 	 stream's contents is garbage anyway.  */
       if (fputc ('\0', fp) == EOF)
-	return -1; /* errno is set here */
+	goto close_preserving_errno; /* errno is set here */
       if (fflush (fp))
-	return -1; /* errno is set here */
+	goto close_preserving_errno; /* errno is set here */
       /* Give up on errno.  */
       errno = 0;
-      return -1;
+     close_preserving_errno:
+      /* There's an error.  Nevertheless call fclose(fp), for consistency
+	 with the other cases.  */
+      {
+	int saved_errno = errno;
+	fclose (fp);
+	errno = saved_errno;
+	return -1;
+      }
     }
 
   /* If we are closing stdout, don't attempt to do it later again.  */