bug-gnulib
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: undefined behavior in closeout, aggravated by libsigsegv


From: Bruno Haible
Subject: Re: undefined behavior in closeout, aggravated by libsigsegv
Date: Sun, 22 Nov 2009 13:32:51 +0100
User-agent: KMail/1.9.9

Eric Blake wrote on 2009-07-18:
> fileno(stdout) == 1 by definition of STDOUT_FILENO, so you don't
> have to go via fileno (you can directly use fcntl(1,...) to learn whether
> the fd has been closed).

But the user might have called freopen (..., stdout). In this case,
fileno (stdout) will no longer be 1. So you actually do have to use
fileno (stdout) in the patch that you committed on 2009-07-18
<http://lists.gnu.org/archive/html/bug-gnulib/2009-07/msg00040.html>

Here is a proposed patch:


2009-11-22  Bruno Haible  <address@hidden>

        error: account for the possibility of freopen (stdout).
        * lib/error.c (error): Use fileno (stdout) instead of 1.

*** lib/error.c.orig    2009-11-22 13:30:20.000000000 +0100
--- lib/error.c 2009-11-22 13:30:05.000000000 +0100
***************
*** 238,250 ****
                   0);
  #endif
  
  #if !_LIBC && defined F_GETFL
!   /* POSIX states that fflush (stdout) after fclose is unspecified; it
!      is safe in glibc, but not on all other platforms.  fflush (NULL)
!      is always defined, but too draconian.  */
!   if (0 <= fcntl (1, F_GETFL))
  #endif
!   fflush (stdout);
  #ifdef _LIBC
    _IO_flockfile (stderr);
  #endif
--- 238,259 ----
                   0);
  #endif
  
+   {
  #if !_LIBC && defined F_GETFL
!     int stdout_fd;
! 
!     /* POSIX states that fileno (stdout) after fclose is unspecified.  But in
!        practice it is not a problem, because stdout is statically allocated 
and
!        the fd of a FILE stream is stored as a field in its allocated memory.  
*/
!     stdout_fd = fileno (stdout);
!     /* POSIX states that fflush (stdout) after fclose is unspecified; it
!        is safe in glibc, but not on all other platforms.  fflush (NULL)
!        is always defined, but too draconian.  */
!     if (0 <= stdout_fd && 0 <= fcntl (stdout_fd, F_GETFL))
  #endif
!       fflush (stdout);
!   }
! 
  #ifdef _LIBC
    _IO_flockfile (stderr);
  #endif




reply via email to

[Prev in Thread] Current Thread [Next in Thread]