fixdep: use fflush() and ferror() to ensure successful write to files
authorMasahiro Yamada <masahiroy@kernel.org>
Sun, 6 Mar 2022 07:25:35 +0000 (16:25 +0900)
committerMasahiro Yamada <masahiroy@kernel.org>
Thu, 31 Mar 2022 03:03:46 +0000 (12:03 +0900)
commit69304379ff036ce8ecf41efc2aeea4b29dd0c43f
tree4c92064e4eff53b54c9b4afbc5f9c2f20c05ad1f
parentbbc90bc1bd4a63121bae9cbfafe1e1f0beaf24b1
fixdep: use fflush() and ferror() to ensure successful write to files

Currently, fixdep checks the return value from (v)printf(), but it does
not ensure the complete write to the .cmd file.

printf() just writes data to the internal buffer, which usually succeeds.
(Of course, it may fail for another reason, for example when the file
descriptor is closed, but that is another story.)

When the buffer (4k?) is full, an actual write occurs, and printf() may
really fail. One of typical cases is "No space left on device" when the
disk is full.

The data remaining in the buffer will be pushed out to the file when
the program exits, but we never know if it is successful.

One straight-forward fix would be to add the following code at the end
of the program.

   ret = fflush(stdout);
   if (ret < 0) {
          /* error handling */
   }

However, it is tedious to check the return code in all the call sites
of printf(), fflush(), fclose(), and whatever can cause actual writes
to the end device. Doing that lets the program bail out at the first
failure but is usually not worth the effort.

Instead, let's check the error status from ferror(). This is 'sticky',
so you need to check it just once. You still need to call fflush().

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
Reviewed-by: David Laight <david.laight@aculab.com>
Reviewed-by: Nick Desaulniers <ndesaulniers@google.com>
scripts/basic/fixdep.c