|
| 1 | +/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8 |
| 2 | +// test_stream_error_position.cpp |
| 3 | + |
| 4 | +// Copyright 2026 Gennaro Prota. |
| 5 | +// Distributed under the Boost Software License, Version 1.0. |
| 6 | +// (See accompanying file LICENSE_1_0.txt or copy at |
| 7 | +// http://www.boost.org/LICENSE_1_0.txt) |
| 8 | + |
| 9 | +// See http://www.boost.org for updates, documentation, and revision history. |
| 10 | + |
| 11 | +// An input_stream_error used to say only that the input had failed. The |
| 12 | +// exception now names the offset the input went wrong at. |
| 13 | + |
| 14 | +// Suggested by swebb2066 in |
| 15 | +// https://github.com/boostorg/serialization/pull/318. Thanks! |
| 16 | + |
| 17 | +// The offset itself is not checked against a fixed number: it moves with the |
| 18 | +// archive version, and with the width of whatever the header happens to hold. |
| 19 | +// What has to hold is that an offset is reported at all, that it is a number, |
| 20 | +// and that it falls inside the archive. |
| 21 | + |
| 22 | +#include <cstddef> |
| 23 | +#include <cstdlib> // atol |
| 24 | +#include <sstream> |
| 25 | +#include <string> |
| 26 | + |
| 27 | +// The lightweight test of Boost.Core is used here in place of |
| 28 | +// test_tools.hpp, whose test_main runs one archive type per build. Every |
| 29 | +// archive type has to be exercised in the same run, because each reaches the |
| 30 | +// offset by a different route: the text primitive through a failed |
| 31 | +// extraction, the binary one through a short read, and the XML one through a |
| 32 | +// parse which ran out of input. |
| 33 | +#include <boost/core/lightweight_test.hpp> |
| 34 | + |
| 35 | +#include <boost/archive/archive_exception.hpp> |
| 36 | +#include <boost/archive/binary_iarchive.hpp> |
| 37 | +#include <boost/archive/binary_oarchive.hpp> |
| 38 | +#include <boost/archive/text_iarchive.hpp> |
| 39 | +#include <boost/archive/text_oarchive.hpp> |
| 40 | +#include <boost/archive/xml_iarchive.hpp> |
| 41 | +#include <boost/archive/xml_oarchive.hpp> |
| 42 | +#include <boost/serialization/nvp.hpp> |
| 43 | + |
| 44 | +struct pair_of_ints { |
| 45 | + int x; |
| 46 | + int y; |
| 47 | + pair_of_ints() : x(0), y(0) {} |
| 48 | + template<class Archive> |
| 49 | + void serialize(Archive & ar, const unsigned int /* version */){ |
| 50 | + ar & BOOST_SERIALIZATION_NVP(x); |
| 51 | + ar & BOOST_SERIALIZATION_NVP(y); |
| 52 | + } |
| 53 | +}; |
| 54 | + |
| 55 | +// Returns the offset named by the message, or -1 if it names none. A |
| 56 | +// message reads "input stream error-at offset 36". |
| 57 | +static long reported_offset(const std::string & message){ |
| 58 | + const std::string tag("at offset "); |
| 59 | + const std::size_t at = message.find(tag); |
| 60 | + if(std::string::npos == at){ |
| 61 | + return -1; |
| 62 | + } |
| 63 | + const std::string digits = message.substr(at + tag.size()); |
| 64 | + if(digits.empty() || '0' > digits[0] || '9' < digits[0]){ |
| 65 | + return -1; |
| 66 | + } |
| 67 | + return std::atol(digits.c_str()); |
| 68 | +} |
| 69 | + |
| 70 | +template<class OArchive> |
| 71 | +static std::string archive_of_a_pair(){ |
| 72 | + std::ostringstream os; |
| 73 | + { |
| 74 | + OArchive oa(os); |
| 75 | + pair_of_ints p; |
| 76 | + p.x = 11; |
| 77 | + p.y = 22; |
| 78 | + oa << BOOST_SERIALIZATION_NVP(p); |
| 79 | + } |
| 80 | + return os.str(); |
| 81 | +} |
| 82 | + |
| 83 | +// Loads the given text as an archive and returns the offset the resulting |
| 84 | +// input_stream_error names, or -1 if the load did not fail that way. |
| 85 | +template<class IArchive> |
| 86 | +static long offset_of_failure(const std::string & data){ |
| 87 | + BOOST_TRY { |
| 88 | + std::istringstream is(data); |
| 89 | + IArchive ia(is); |
| 90 | + pair_of_ints p; |
| 91 | + ia >> BOOST_SERIALIZATION_NVP(p); |
| 92 | + } |
| 93 | + BOOST_CATCH(const boost::archive::archive_exception & e){ |
| 94 | + if(boost::archive::archive_exception::input_stream_error != e.code){ |
| 95 | + return -1; |
| 96 | + } |
| 97 | + return reported_offset(e.what()); |
| 98 | + } |
| 99 | + BOOST_CATCH_END |
| 100 | + return -1; |
| 101 | +} |
| 102 | + |
| 103 | +// A value the archive cannot read back, put where the second int was |
| 104 | +// written. The text and binary archives keep the last "22" for that int; |
| 105 | +// the XML one writes it before the closing tags, so the first occurrence is |
| 106 | +// the one to spoil. |
| 107 | +template<class OArchive, class IArchive> |
| 108 | +static void test_corrupted_value(bool from_front){ |
| 109 | + std::string data = archive_of_a_pair<OArchive>(); |
| 110 | + const std::size_t at = from_front ? data.find("22") : data.rfind("22"); |
| 111 | + BOOST_TEST(std::string::npos != at); |
| 112 | + data.replace(at, 2, "zz"); |
| 113 | + |
| 114 | + const long offset = offset_of_failure<IArchive>(data); |
| 115 | + BOOST_TEST_GT(offset, 0); |
| 116 | + BOOST_TEST_LE(offset, static_cast<long>(data.size())); |
| 117 | +} |
| 118 | + |
| 119 | +// An archive cut short, so that the failure comes from running out of input |
| 120 | +// rather than from a value which will not parse. |
| 121 | +template<class OArchive, class IArchive> |
| 122 | +static void test_truncated(std::size_t chop){ |
| 123 | + std::string data = archive_of_a_pair<OArchive>(); |
| 124 | + BOOST_TEST_GT(data.size(), chop); |
| 125 | + data.resize(data.size() - chop); |
| 126 | + |
| 127 | + const long offset = offset_of_failure<IArchive>(data); |
| 128 | + BOOST_TEST_GT(offset, 0); |
| 129 | + BOOST_TEST_LE(offset, static_cast<long>(data.size())); |
| 130 | +} |
| 131 | + |
| 132 | +int |
| 133 | +main(){ |
| 134 | + using namespace boost::archive; |
| 135 | + |
| 136 | + test_corrupted_value<text_oarchive, text_iarchive>(false); |
| 137 | + test_corrupted_value<xml_oarchive, xml_iarchive>(true); |
| 138 | + |
| 139 | + test_truncated<binary_oarchive, binary_iarchive>(6); |
| 140 | + test_truncated<xml_oarchive, xml_iarchive>(30); |
| 141 | + |
| 142 | + return boost::report_errors(); |
| 143 | +} |
0 commit comments