On Sat, Jul 30, 2022 at 05:23:37PM -0600, Todd C. Miller wrote:
> On Sat, 30 Jul 2022 18:19:02 -0500, Scott Cheloha wrote:
> 
> > Bump.  The standard's error cases for fflush(3) are identical to those
> > for fclose(3):
> >
> > https://pubs.opengroup.org/onlinepubs/9699919799/functions/fflush.html
> > https://pubs.opengroup.org/onlinepubs/9699919799/functions/fclose.html
> >
> > Is the fact that our fclose(3) can succeed even if the error flag is
> > set a bug?
> 
> As far as I can tell, neither fflush() nor fclose() check the status
> of the error flag, though they may set it of course.  That is why
> I was suggesting an explicit ferror() call at the end.

I'm sorry, I'm having a dumb moment, I don't quite understand what
you're looking for.

Please tweak my patch so it's the way you want it, with the ferror(3)
call in the right spot.

We're sorta-kinda circling around adding the missing (?) stdio error
checking to other utilities in bin/ and usr.bin/, no?  I want to be
sure I understand how to do the next patch, because if we do that it
will probably be a bunch of programs all at once.

Index: echo.c
===================================================================
RCS file: /cvs/src/bin/echo/echo.c,v
retrieving revision 1.10
diff -u -p -r1.10 echo.c
--- echo.c      9 Oct 2015 01:37:06 -0000       1.10
+++ echo.c      10 Aug 2022 18:00:12 -0000
@@ -53,12 +53,15 @@ main(int argc, char *argv[])
                nflag = 0;
 
        while (*argv) {
-               (void)fputs(*argv, stdout);
-               if (*++argv)
-                       putchar(' ');
+               if (fputs(*argv, stdout) == EOF)
+                       err(1, "stdout");
+               if (*++argv && putchar(' ') == EOF)
+                       err(1, "stdout");
        }
-       if (!nflag)
-               putchar('\n');
+       if (!nflag && putchar('\n') == EOF)
+               err(1, "stdout");
+       if (fflush(stdout) == EOF || ferror(stdout) || fclose(stdout) == EOF)
+               err(1, "stdout");
 
        return 0;
 }

Reply via email to