[Boost-bugs] [Boost C++ Libraries] #1849: Deserialization of std::string overwrites non-copied contents.

Subject: [Boost-bugs] [Boost C++ Libraries] #1849: Deserialization of std::string overwrites non-copied contents.
From: Boost C++ Libraries (noreply_at_[hidden])
Date: 2008-04-22 21:40:21


#1849: Deserialization of std::string overwrites non-copied contents.
------------------------------------------------------------+---------------
 Reporter: Siegfried Kettlitz <dev_at_[hidden]> | Owner: ramey
     Type: Bugs | Status: new
Milestone: Boost 1.35.1 | Component: serialization
  Version: Boost 1.35.0 | Severity: Showstopper
 Keywords: |
------------------------------------------------------------+---------------
 When a string is initialized with another string it doesn't copy the other
 strings contents but refers to it:
 {{{
     const std::string Original( "Hello" );
     std::string Foostring = Original; // Lazy copy.
 }}}

 When that copied string is deserialized, it overwrites the original
 string's contents. Instead it should always allocate new memory for a
 string when deserializing.

 One workaround is to initialize the string with the c_str() of the
 original string to enforce a copy:
 {{{
     std::string Foostring = Original.c_str(); // Really copy the string.
 }}}

 The example was tested on Ubuntu 8.04 with gcc 4.2.3.

 ----
 Code:
 {{{
 #include <string>
 #include <iostream>
 #include <sstream>

 #include <boost/archive/binary_oarchive.hpp>
 #include <boost/archive/binary_iarchive.hpp>

 #include <boost/serialization/base_object.hpp>
 #include <boost/serialization/utility.hpp>
 #include <boost/serialization/vector.hpp>

 void Test1()
 {
     const std::string Original( "Hello" );
     std::string Foostring = Original; // Lazy copy.
     std::string Barstring( "BarST" );

     std::cout
         << "Original:" << Original
         << " Foostring:" << Foostring
         << " Barstring:" << Barstring
         << std::endl;


     std::ostringstream OS(std::ios::binary);
     boost::archive::binary_oarchive OA( OS );
     OA << Barstring;

     std::istringstream IS( OS.str(), std::ios::binary);
     boost::archive::binary_iarchive IA( IS );
     IA >> Foostring;

     std::cout
         << "Original:" << Original
         << " Foostring:" << Foostring
         << " Barstring:" << Barstring
         << std::endl;

 }

 void Test2()
 {
     const std::string Original( "Hello" );
     std::string Foostring = Original.c_str(); // Really copy the string.
     std::string Barstring( "BarST" );

     std::cout
         << "Original:" << Original
         << " Foostring:" << Foostring
         << " Barstring:" << Barstring
         << std::endl;


     std::ostringstream OS(std::ios::binary);
     boost::archive::binary_oarchive OA( OS );
     OA << Barstring;

     std::istringstream IS( OS.str(), std::ios::binary);
     boost::archive::binary_iarchive IA( IS );
     IA >> Foostring;

     std::cout
         << "Original:" << Original
         << " Foostring:" << Foostring
         << " Barstring:" << Barstring
         << std::endl;

 }


 int main()
 {
     std::cout << "Test1()" << std::endl;
     Test1();
     std::cout << "Test2()" << std::endl;
     Test2();
 }
 }}}

 ----

 Output:
 {{{
 Test1()
 Original:Hello Foostring:Hello Barstring:BarST
 Original:BarST Foostring:BarST Barstring:BarST
 Test2()
 Original:Hello Foostring:Hello Barstring:BarST
 Original:Hello Foostring:BarST Barstring:BarST
 }}}

--
Ticket URL: <http://svn.boost.org/trac/boost/ticket/1849>
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:57 UTC