【问题标题】:c++ boost serialization how to prevent crashes with improper filesc ++ boost序列化如何防止不正确的文件崩溃
【发布时间】:2014-09-03 17:07:45
【问题描述】:

我正在使用 boosts 序列化函数来序列化数据并将其保存到项目中的外部文件,然后能够再次读取它们。

我遇到的问题是,如果以不正确的格式(错误地)读取文件,则程序会崩溃(可能是预期的)。

如何识别文件格式错误并在程序崩溃之前中断读入过程?

编辑:

我已经在我的读入函数中尝试了一个 try catch 结构,看起来像这样

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;

}

当文件与它试图读入的结构无关时,这会捕获一个异常。但是,当我使用过期版本时,它不会捕获异常并在 'ia >> (*object).content;' 行崩溃有什么想法吗?

【问题讨论】:

    标签: c++ serialization boost


    【解决方案1】:

    它不应该崩溃。如果是,请向库开发人员报告错误。

    应该引发异常。因此,您可以尝试捕获存档异常:

    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
    

    另外,捕捉std::exception&amp; 为例如bad_allocrange_error

    【讨论】:

    • 如果文件是二进制格式并且程序的结构与存储的结构不匹配(除非考虑版本并采取预防措施),它将不可避免地崩溃。
    • @DieterLücking 我认为说“它将不可避免地崩溃”是不公平的。输入卫生存在并且完全适用于二进制输入。它只需要注意细节。我不确定内置的东西,但我完全期待std::bad_alloc,例如当错误地为一个巨大的向量保留时,一个很好的存档异常或std::basic_istream::failure,如果你到达意外的文件结尾、缺少分隔符、损坏的类型代码......
    • @user3353819 你能给我们一个失败的例子,也许是一个触发它的文件?您可能发现了值得报告的错误/漏洞
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    • 2021-04-19
    • 2011-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多