[Boost-bugs] [Boost C++ Libraries] #7873: C4244 in basic_binary_iprimitive.hpp due to std::size_t and std::streamsize type difference

Subject: [Boost-bugs] [Boost C++ Libraries] #7873: C4244 in basic_binary_iprimitive.hpp due to std::size_t and std::streamsize type difference
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2013-01-09 22:20:43


#7873: C4244 in basic_binary_iprimitive.hpp due to std::size_t and std::streamsize
type difference
-----------------------------------------------+----------------------------
 Reporter: Hamp <boost@…> | Owner: ramey
     Type: Bugs | Status: new
Milestone: To Be Determined | Component: serialization
  Version: Boost 1.50.0 | Severity: Cosmetic
 Keywords: |
-----------------------------------------------+----------------------------
 With Visual Studio 2010 a C4244 warning appears in
 basic_binary_iprimitive.hpp.

 boost/archive/basic_binary_iprimitive.hpp(181): warning C4244: 'argument'
 : conversion from 'std::streamsize' to 'size_t', possible loss of data

 The offending line is the last one in the function, with (count - s)

 {{{
 template<class Archive, class Elem, class Tr>
 inline void
 basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
     void *address,
     std::size_t count
 ){
     // note: an optimizer should eliminate the following for char files
     BOOST_ASSERT(
         static_cast<std::streamsize>(count / sizeof(Elem))
         <= boost::integer_traits<std::streamsize>::const_max
     );
     std::streamsize s = static_cast<std::streamsize>(count /
 sizeof(Elem));
     std::streamsize scount = m_sb.sgetn(
         static_cast<Elem *>(address),
         s
     );
     if(scount != s)
         boost::serialization::throw_exception(
             archive_exception(archive_exception::input_stream_error)
         );
     // note: an optimizer should eliminate the following for char files
     BOOST_ASSERT(count % sizeof(Elem) <=
 boost::integer_traits<std::streamsize>::const_max);
     s = static_cast<std::streamsize>(count % sizeof(Elem));
     if(0 < s){
 // if(is.fail())
 // boost::serialization::throw_exception(
 // archive_exception(archive_exception::stream_error)
 // );
         Elem t;
         scount = m_sb.sgetn(& t, 1);
         if(scount != 1)
             boost::serialization::throw_exception(
                 archive_exception(archive_exception::input_stream_error)
             );
         std::memcpy(static_cast<char*>(address) + (count - s), &t, s);
     }
 }
 }}}

 where count is a passed-in std::size_t and s is a local std::streamsize.

 Ticket #3755 discusses a related issue in the asserts where a
 signed/unsigned comparison took place. This was resolved by a static_cast
 in the assert.

 For Visual Studio 2010, x86, and both Release and Debug, the following
 '''effective''' typedefs apply


 {{{
 typedef __int64 streamsize;
 typedef _unsigned int size_t;
 }}}

 Since s is set by count, there does not seen to be a risk of overflowing
 s. Is there any way to prevent the warning?

 I check the current trunk and the issue would still exist.

-- 
Ticket URL: <https://svn.boost.org/trac/boost/ticket/7873>
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:50:11 UTC