From: David Laight <David.Laight@ACULAB.COM>
To: 'Masahiro Yamada' <masahiroy@kernel.org>
Cc: Nick Desaulniers <ndesaulniers@google.com>,
Linux Kbuild mailing list <linux-kbuild@vger.kernel.org>,
LKML <linux-kernel@vger.kernel.org>,
"Michal Marek" <michal.lkml@markovi.net>
Subject: RE: [PATCH v2] fixdep: use fflush() and ferror() to ensure successful write to files
Date: Tue, 1 Mar 2022 09:48:28 +0000 [thread overview]
Message-ID: <723970e562d04b22899fe3855d599cab@AcuMS.aculab.com> (raw)
In-Reply-To: <CAK7LNARKxd-kr3pABzTC2+uGhEKbyLtXDCSoxn56P0go--bg1A@mail.gmail.com>
From: Masahiro Yamada
> Sent: 01 March 2022 09:06
>
> On Tue, Mar 1, 2022 at 11:28 AM David Laight <David.Laight@aculab.com> wrote:
> >
> > The return value from fprintf() is normally the number of bytes written to
> > the internal buffer (8k in glibc?)
> >
> > Only if the buffer is full and an actual write() is done do you get any indication of an error.
> >
> > So you can use the error return from fprintf() to terminate a loop – but it usually
> > just isn’t worth the effort.
> >
> > The error status returned by ferror() is ‘sticky’, so you need only check once.
> >
> > But you need to check before fclose().
> >
> > Since fclose() has to write out the buffer – that write can also fail.
> >
> > I’m not sure whether fclose() returns and error in that case, but adding fflush()
> > makes the coding easier.
>
>
> I just checked this.
>
> fclose() returns -1 if it fails to flush the buffer.
But you still need to check ferror() before it.
So you might as well do fflush(); if (ferror()); fclose();
Then there is only one error exit path.
David
-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)
prev parent reply other threads:[~2022-03-01 9:48 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-02-25 14:42 [PATCH v2] fixdep: use fflush() and ferror() to ensure successful write to files Masahiro Yamada
[not found] ` <CAKwvOd=WjnHSHKLVRJifHxV2tyDsLTkek80NWU=do=FSHhNLug@mail.gmail.com>
[not found] ` <67b75a36cf874dfea0871649ccd268d3@AcuMS.aculab.com>
2022-03-01 8:21 ` Masahiro Yamada
2022-03-01 18:36 ` Nick Desaulniers
2022-03-01 9:06 ` Masahiro Yamada
2022-03-01 9:48 ` David Laight [this message]
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=723970e562d04b22899fe3855d599cab@AcuMS.aculab.com \
--to=david.laight@aculab.com \
--cc=linux-kbuild@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=masahiroy@kernel.org \
--cc=michal.lkml@markovi.net \
--cc=ndesaulniers@google.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox