This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: stderr vs. STDERR_FILENO


Zack Weinberg <zack at codesourcery dot com> writes:
> For what it's worth I think this is primarily a documentation issue -

It just occured to me -- no, unfortunately, this is a correctness issue.

This C++ program is well-defined by the standard -- it invokes
std::terminate, which invokes abort().

#include <cstdio>
#include <stdexcept>

int main()
{
  std::fclose(stderr);

  throw std::runtime_error("Boom!");
}
However, given a general-purpose C library which complies only with the
requirements of the C/C++ standards, __gnu_cxx::__verbose_terminate_handler
invokes undefined behavior on this program.

Now, as a matter of QoI, I suspect that in almost every actual C library a
write to a closed stderr will always fail with EOF, which means that my
little program still has the behavior mandated by the standard.

I suggest that, in addition to Zack's warning documentation, there be a
target-specific configuration flag for libstdc++ which specifies that the
underlying C library doesn't support writes to stderr after fclose.  In
those cases, std::terminate should just be abort().

-- 
Jonathan Lennox
lennox at cs dot columbia dot edu

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]