• Masahiro Yamada's avatar
    fixdep: use fflush() and ferror() to ensure successful write to files · 69304379
    Masahiro Yamada authored
    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: default avatarMasahiro Yamada <masahiroy@kernel.org>
    Reviewed-by: default avatarDavid Laight <david.laight@aculab.com>
    Reviewed-by: default avatarNick Desaulniers <ndesaulniers@google.com>
    69304379
fixdep.c 9.09 KB