Opened 10 years ago
Closed 10 years ago
#8164 closed Bugs (duplicate)
basic_text_iprimitive loads garbage instead of throwing
Reported by: | Owned by: | Robert Ramey | |
---|---|---|---|
Milestone: | To Be Determined | Component: | serialization |
Version: | Boost 1.53.0 | Severity: | Problem |
Keywords: | serialization archive_exception input_stream_error | Cc: |
Description
Hi,
When loading several objects from a text_iarchive, one can load one too many object without the archive throwing an exception. In such case, the last object loaded will contain garbage. If one tries to load yet another object, then an exception is thrown. Here is a minimal working example to show the issue:
#include <boost/archive/text_iarchive.hpp> #include <boost/archive/text_oarchive.hpp> #include <iostream> #include <sstream> int main() { std::stringstream ss; { boost::archive::text_oarchive oarchive(ss); char a = 'a', b = 'b'; oarchive << a << b; } std::cout << "saved: \"" << ss.str() << "\"\n"; { boost::archive::text_iarchive iarchive(ss); char a, b, garbage, will_fail; iarchive >> a; std::cout << "happily loaded a:\"" << a << "\"\n"; iarchive >> b; std::cout << "happily loaded b:\"" << b << "\"\n"; iarchive >> garbage; std::cout << "happily loaded garbage:\"" << garbage << "\"\n"; try { iarchive >> will_fail; } catch (std::exception const& e) { std::cout << e.what(); throw; } } }
Attached is a patch that seems to fix the problem (and removes some trailing white spaces from the sources). I also ran all of Boost.Serialization's unit tests with the fix and everything passes except for test_array_xml_warchive, which fails with something weird and probably unrelated.
Regards,
Louis Dionne