user3353819
user3353819

Reputation: 939

c++ boost serialization how to prevent crashes with improper files

I'm using boosts serialization functions to serialize and save data to external files in my project and then to be able to read them in again.

The issue I'm having is that if a file is read in (in error) not in the correct format then the program crashes (perhaps expectedly).

How can I identify that the file is in the wrong format and interrupt the read in process before the program crashses?

EDIT:

I've tried a try catch construct in my read in function which looks like this

int read_binary(file_in_out* object){

    std::ifstream ifs((*object).file.c_str());

    try
    {
        boost::archive::binary_iarchive ia(ifs);
        ia >> (*object).content;


    }
    catch (boost::archive::archive_exception& ex) 
    {
        std::cout << "An exception occurred. Exception Nr. "  << '\n';
        return 1;
    }
    catch (int e)
    {
        std::cout << "An exception occurred. Exception Nr. " << e << '\n';
        return 1;
    }

    return 0;

}

This catches an exception when the file is nothing to do with the structures that it is trying to read into it. However, when I use an out of date version it doesn't catch an exception and crashes on line 'ia >> (*object).content;' Any ideas?

Upvotes: 4

Views: 1499

Answers (1)

sehe
sehe

Reputation: 393239

It should not crash. If it does, report a bug with the library developers.

It should raise exceptions though. So, you could try to catch archive exceptions:

http://www.boost.org/doc/libs/1_36_0/libs/serialization/doc/exceptions.html

    unregistered_class,     // attempt to serialize a pointer of an
                            // an unregistered class
    invalid_signature,      // first line of archive does not contain
                            // expected string
    unsupported_version,    // archive created with library version subsequent
                            // to this one
    pointer_conflict        // an attempt has been made to directly serialize
                            // an object after having already serialized the same
                            // object through a pointer.  Were this permitted, 
                            // it the archive load would result in the creation
                            // of extraneous object.
    incompatible_native_format, // attempt to read native binary format
                            // on incompatible platform
    array_size_too_short,   // array being loaded doesn't fit in array allocated
    stream_error            // i/o error on stream
    invalid_class_name,     // class name greater than the maximum permitted.
                            // most likely a corrupted archive or an attempt
                            // to insert virus via buffer overrun method.
    unregistered_cast       // base - derived relationship not registered with 
                            // void_cast_register

Also, catch std::exception& for e.g. bad_alloc, range_error etc.

Upvotes: 1

Related Questions