From f3000fe73349f65bb7cc433fb5dccec2bce8d3b8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Mon, 24 Apr 2006 11:32:00 +0000 Subject: [PATCH] Call fclose() in all cases, even in the failure case. --- lib/ChangeLog | 6 ++++++ lib/fwriteerror.c | 18 +++++++++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/lib/ChangeLog b/lib/ChangeLog index 19a49cc846..8de8bf1151 100644 --- a/lib/ChangeLog +++ b/lib/ChangeLog @@ -1,3 +1,9 @@ +2006-04-23 Bruno Haible + + * fwriteerror.c (fwriteerror): Call fclose also when an error + condition was already detected. + Reported by Ben Pfaff . + 2006-04-19 Derek Price Eric Blake diff --git a/lib/fwriteerror.c b/lib/fwriteerror.c index a88c8e1c39..f010674ba4 100644 --- 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 , 2003. This program is free software; you can redistribute it and/or modify @@ -47,18 +47,26 @@ fwriteerror (FILE *fp) 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. */ -- 2.30.2