Boost C++ Libraries: Ticket #3118: [Serialization] basic_binary_oarchive.hpp - save_override possible loss of data warnings https://svn.boost.org/trac10/ticket/3118 <p> The following save_override overloads cause possible loss of data warnings on MSVC8 and XCode3.1.2/gcc4.0.1 </p> <blockquote> <p> void save_override(const version_type &amp; t, int) void save_override(const class_id_type &amp; t, int) void save_override(const class_id_reference_type &amp; t, int) </p> </blockquote> <p> with their respective assignments: </p> <blockquote> <p> const unsigned char x = t.t; const int_least16_t x = t.t; </p> </blockquote> <p> While a possible fix would be: </p> <blockquote> <p> const unsigned char x = static_cast&lt;unsigned char&gt;(t.t); const int_least16_t x = static_cast&lt;int_least16_t&gt;(t.t); </p> </blockquote> <p> there is still a possibility of a silent loss of data. </p> <p> We could be safer and use numeric_cast, but that would possibly impact code size and performance. </p> <p> Why are the xxx_type strong typedef's using int rather than the smaller types that are being serialized? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3118 Trac 1.4.3 jflinn@… Tue, 02 Jun 2009 02:03:02 GMT <link>https://svn.boost.org/trac10/ticket/3118#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/3118#comment:1</guid> <description> <p> Index: basic_binary_oarchive.hpp =================================================================== --- basic_binary_oarchive.hpp (revision 53559) +++ basic_binary_oarchive.hpp (working copy) @@ -30,6 +30,7 @@ </p> <blockquote> <p> #include &lt;boost/archive/detail/common_oarchive.hpp&gt; #include &lt;boost/serialization/string.hpp&gt; #include &lt;boost/serialization/collection_size_type.hpp&gt; </p> </blockquote> <p> +#include &lt;boost/integer_traits.hpp&gt; </p> <blockquote> <p> namespace boost { namespace archive { </p> </blockquote> <p> @@ -69,17 +70,20 @@ </p> <blockquote> <p> void save_override(const version_type &amp; t, int){ </p> <blockquote> <p> <em> upto 255 versions </em> note:t.t resolves borland ambguity </p> </blockquote> </blockquote> <ul><li> const unsigned char x = t.t; </li></ul><p> + assert(t.t &lt;= boost::integer_traits&lt;unsigned char&gt;::const_max); + const unsigned char x = static_cast&lt;unsigned char&gt;(t.t); </p> <ul><li>this-&gt;This() &lt;&lt; x; </li></ul><blockquote> <p> } void save_override(const class_id_type &amp; t, int){ </p> <blockquote> <p> <em> upto 32K classes </em></p> </blockquote> </blockquote> <ul><li> const int_least16_t x = t.t; </li></ul><p> + assert(t.t &lt;= boost::integer_traits&lt;int_least16_t&gt;::const_max); + const int_least16_t x = static_cast&lt;int_least16_t&gt;(t.t); </p> <ul><li>this-&gt;This() &lt;&lt; x; </li></ul><blockquote> <p> } void save_override(const class_id_reference_type &amp; t, int){ </p> <blockquote> <p> <em> upto 32K classes </em></p> </blockquote> </blockquote> <ul><li> const int_least16_t x = t.t; </li></ul><p> + assert(t.t &lt;= boost::integer_traits&lt;int_least16_t&gt;::const_max); + const int_least16_t x = static_cast&lt;int_least16_t&gt;(t.t); </p> <ul><li>this-&gt;This() &lt;&lt; x; </li></ul><blockquote> <p> } void save_override(const object_id_type &amp; t, int){ </p> </blockquote> </description> <category>Ticket</category> </item> <item> <dc:creator>Robert Ramey</dc:creator> <pubDate>Sat, 06 Jun 2009 02:11:58 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/3118#comment:2 https://svn.boost.org/trac10/ticket/3118#comment:2 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">fixed</span> </li> </ul> Ticket