[Boost-bugs] [Boost C++ Libraries] #2542: filesystem: wrong close 0 descriptor in copy_file_api on unix

Subject: [Boost-bugs] [Boost C++ Libraries] #2542: filesystem: wrong close 0 descriptor in copy_file_api on unix
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-11-27 14:20:54


#2542: filesystem: wrong close 0 descriptor in copy_file_api on unix
--------------------------+-------------------------------------------------
 Reporter: ai_at_[hidden] | Type: Bugs
   Status: new | Milestone: Boost 1.38.0
Component: None | Version: Boost 1.37.0
 Severity: Problem | Keywords:
--------------------------+-------------------------------------------------
 This code is wrong if ::stat call fails - it is just close descriptor with
 number 0 (by if ( infile >= 0 ) ::close( infile );).

       BOOST_FILESYSTEM_DECL error_code
       copy_file_api( const std::string & from_file_ph,
         const std::string & to_file_ph )
       {
         const std::size_t buf_sz = 32768;
         boost::scoped_array<char> buf( new char [buf_sz] );
         int infile=0, outfile=0; // init quiets compiler warning
         struct stat from_stat;

         if ( ::stat( from_file_ph.c_str(), &from_stat ) != 0
           || (infile = ::open( from_file_ph.c_str(),
                               O_RDONLY )) < 0
           || (outfile = ::open( to_file_ph.c_str(),
                                 O_WRONLY | O_CREAT | O_EXCL,
                                 from_stat.st_mode )) < 0 )
         {
           if ( infile >= 0 ) ::close( infile );
           return error_code( errno, system_category );
         }


 Fix is just changing
        int infile=0, outfile=0; // init quiets compiler warning
 to
        int infile=-1, outfile=-1;

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/2542>
Boost C++ Libraries <http://www.boost.org/>
Boost provides free peer-reviewed portable C++ source libraries.

This archive was generated by hypermail 2.1.7 : 2017-02-16 18:49:59 UTC