Opened 13 years ago

Closed 13 years ago

#3118 closed Bugs (fixed)

[Serialization] basic_binary_oarchive.hpp - save_override possible loss of data warnings

Reported by: jflinn@… Owned by: Robert Ramey
Milestone: Boost 1.40.0 Component: serialization
Version: Boost 1.39.0 Severity: Problem
Keywords: Serialization, warning Cc:

Description

The following save_override overloads cause possible loss of data warnings on MSVC8 and XCode3.1.2/gcc4.0.1

void save_override(const version_type & t, int) void save_override(const class_id_type & t, int) void save_override(const class_id_reference_type & t, int)

with their respective assignments:

const unsigned char x = t.t; const int_least16_t x = t.t;

While a possible fix would be:

const unsigned char x = static_cast<unsigned char>(t.t); const int_least16_t x = static_cast<int_least16_t>(t.t);

there is still a possibility of a silent loss of data.

We could be safer and use numeric_cast, but that would possibly impact code size and performance.

Why are the xxx_type strong typedef's using int rather than the smaller types that are being serialized?

Change History (2)

comment:1 by jflinn@…, 13 years ago

Index: basic_binary_oarchive.hpp =================================================================== --- basic_binary_oarchive.hpp (revision 53559) +++ basic_binary_oarchive.hpp (working copy) @@ -30,6 +30,7 @@

#include <boost/archive/detail/common_oarchive.hpp> #include <boost/serialization/string.hpp> #include <boost/serialization/collection_size_type.hpp>

+#include <boost/integer_traits.hpp>

namespace boost { namespace archive {

@@ -69,17 +70,20 @@

void save_override(const version_type & t, int){

upto 255 versions note:t.t resolves borland ambguity

  • const unsigned char x = t.t;

+ assert(t.t <= boost::integer_traits<unsigned char>::const_max); + const unsigned char x = static_cast<unsigned char>(t.t);

  • this->This() << x;

} void save_override(const class_id_type & t, int){

upto 32K classes

  • const int_least16_t x = t.t;

+ assert(t.t <= boost::integer_traits<int_least16_t>::const_max); + const int_least16_t x = static_cast<int_least16_t>(t.t);

  • this->This() << x;

} void save_override(const class_id_reference_type & t, int){

upto 32K classes

  • const int_least16_t x = t.t;

+ assert(t.t <= boost::integer_traits<int_least16_t>::const_max); + const int_least16_t x = static_cast<int_least16_t>(t.t);

  • this->This() << x;

} void save_override(const object_id_type & t, int){

comment:2 by Robert Ramey, 13 years ago

Resolution: fixed
Status: newclosed
Note: See TracTickets for help on using tickets.