Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to deserialize from large binary file #851

Open
beautifulnight opened this issue Jan 15, 2025 · 1 comment
Open

Failed to deserialize from large binary file #851

beautifulnight opened this issue Jan 15, 2025 · 1 comment

Comments

@beautifulnight
Copy link

Description:
When serializing and deserializing a large std::string (10MB) using cereal::BinaryOutputArchive and cereal::BinaryInputArchive (PortableBinaryInputArchive also has this issue), the deserialization process fails with the error:

Failed to read 10485760 bytes from input stream! Read 10485752

Minimal Reproducible Example:

#include "cereal/archives/binary.hpp"
#include <fstream>
#include <string>

struct MyData {
    std::string str;

    template <class Archive>
    void serialize(Archive &archive) {
        archive(str);
    }
};

int main() {
    MyData data;
    const size_t size = 10 * 1024ULL * 1024ULL; // 10MB
    std::string large_str(size, '\0');
    data.str = large_str;

    // Serialize
    {
        std::ofstream ofs("output.bin", std::ios::binary);
        cereal::BinaryOutputArchive bin_out_ar(ofs);
        bin_out_ar(data);
    }

    // Deserialize
    {
        std::ifstream ifs("output.bin", std::ios::binary);
        cereal::BinaryInputArchive bin_in_ar(ifs);
        MyData data2;
        bin_in_ar(data2); // Error occurs here
    }

    return 0;
}

Additional Information:
Cereal Version v1.3.0
MSVC 2017
Windows10
The size of the output.bin file:
image

@unpatel-nvidia
Copy link

close "ofs" handler before deserialization.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants