Opened 4 years ago

#13563 new Patches

Error deserializing empty forward_list

Reported by: Maximiliano Pin <mxcpin@…> Owned by: Robert Ramey
Milestone: To Be Determined Component: serialization
Version: Boost 1.67.0 Severity: Showstopper
Keywords: Cc:

Description

Deserializing an empty forward_list is broken if the elements aren't default constructible (with old compilers it's enough that elements have a non-trivial default constructor).

A patch is attached. The problem is that the first element is always loaded first, even if the list is empty. The result may be a crash or any undefined behavior, since deserialization goes out of sync.

Test case:

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

#include <forward_list>
#include <iostream>
#include <sstream>

struct Foo
{
  int x;
  Foo(int a) : x(a) {};

  template<class Archive>
  void serialize(Archive& ar, const unsigned int) { ar & x; }

private:
  friend class boost::serialization::access;
  Foo() = default;
};

int main()
{
  std::forward_list<Foo> flist;

  std::ostringstream oss;
  boost::archive::binary_oarchive oa{oss};
  oa << flist;

  std::istringstream iss{oss.str()};
  boost::archive::binary_iarchive ia{iss};
  ia >> flist;
}

Results in:

terminate called after throwing an instance of 'boost::archive::archive_exception'
  what():  input stream error
Aborted (core dumped)

Attachments (1)

forward_list.patch (573 bytes ) - added by Maximiliano Pin <mxcpin@…> 4 years ago.

Download all attachments as: .zip

Change History (1)

by Maximiliano Pin <mxcpin@…>, 4 years ago

Attachment: forward_list.patch added
Note: See TracTickets for help on using tickets.