Boost logo

Boost :

Subject: [boost] possible problem with boost::iostreams::file_descriptor
From: Zachary Turner (divisortheory_at_[hidden])
Date: 2009-06-10 20:49:30


nested file_descriptor implementation class is defined as follows:

    struct impl {
        impl() :
            #ifdef BOOST_IOSTREAMS_WINDOWS
                handle_(reinterpret_cast<handle_type>(-1)),
            #else
                handle_(-1),
            #endif
                flags_(0)
            { }
        impl(handle_type fd, bool close_on_exit)
            : handle_(fd), flags_(0)
        { if (close_on_exit) flags_ |= impl::close_on_exit; }
        ~impl()
        { if (flags_ & close_on_exit) close_impl(*this); }
        enum flags {
            close_on_exit = 1,
            append = 4
        };
        handle_type handle_;
        int flags_;
    };

In the second constructor in particular we have this:

        impl(handle_type fd, bool close_on_exit)
            : handle_(fd), flags_(0)
        { if (close_on_exit) flags_ |= impl::close_on_exit; }

I think this should be changed. Just because close_on_exit is specified to
false does not mean a file is not open. It just means don't close it if it
is. I think flags enumeration should be changed to:

        enum flags {
            close_on_exit = 1,
            good = 2,
            append = 4
        };

and constructors / code should be changed accordingly. Otherwise following
code (Windows) asserts, even though I don't think it should:

bool is_open(HANDLE h) { return h && h!=INVALID_HANDLE_VALUE;}

HANDLE h = ::CreateFile("C:\\autoexec.bat", GENERIC_READ,
FILE_SHARE_READ|FILE_SHARE_WRITE,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL,NULL);

boost::iostreams::file_descriptor fd(h, false);

BOOST_ASSERT(fd.is_open() == is_open(h));

Thoughts?


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk