Boost logo

Boost Users :

Subject: [Boost-users] [serialization] Treatment of corrupt archives - assert vs. exception throw
From: Gabriel Redner (gredner_at_[hidden])
Date: 2014-11-12 12:02:47


Hi folks,

I'm dealing with binary archives which may have been corrupted enroute to
my code. My naive expectation was that the serialization library should
throw an exception when it encounters something in the input stream it
doesn't understand (in which case my code can react gracefully). But, I've
found that:

1. There exist cases where serialization will just assert and bring down
the process
2. The documentation suggests that an exception should be thrown, but I
didn't find any strong statement of this, so I am not sure whether it is a
contract of the library.

My first and most pressing question is: is it a guarantee of the library
that invalid input should result in an exception and not an assert? If
this is not so, I think it should be documented much more clearly.

If it is so, then this message is a bug report. There's an assert in
archive/basic_binary_iprimitive.hpp:98 that can be triggered if the bytes
in the archive which represent a bool actually contain a value other than 0
or 1. One can trigger this in a test case like so (on x86_64 Linux, at
least):

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

#include <fstream>

int main()
{
  {
    std::ofstream of("archive.bin", std::ios_base::binary);
    boost::archive::binary_oarchive oa(of);
    unsigned char i = 2;
    oa << i;
  }

  {
    std::ifstream f("archive.bin", std::ios_base::binary);
    boost::archive::binary_iarchive ia(f);
    bool b; // note different type than what was saved, but same size
    ia >> b; // assert!
  }
}
==========

Let me know if this is a bug that I should report to the tracker, or a
feature.

Thanks,
-Gabe



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net