Skip to content

Commit 1ec4277

Browse files
committed
Report the offset of an input stream error
`archive_exception` said only that the input failed, without giving a clue as to where the error occurred. It now names the offset at which the input went wrong, for text, binary and XML input archives alike.
1 parent d772add commit 1ec4277

6 files changed

Lines changed: 233 additions & 7 deletions

File tree

‎include/boost/archive/basic_binary_iprimitive.hpp‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ namespace std{
5555
#include <boost/archive/codecvt_null.hpp>
5656
#include <boost/archive/archive_exception.hpp>
5757
#include <boost/archive/detail/auto_link_archive.hpp>
58+
#include <boost/archive/detail/stream_position_message.hpp>
5859
#include <boost/archive/detail/abi_prefix.hpp> // must be the last header
5960

6061
namespace boost {
@@ -167,10 +168,15 @@ basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
167168
static_cast<Elem *>(address),
168169
s
169170
);
170-
if(scount != s)
171+
if(scount != s){
172+
const std::string message = detail::stream_position_message(& m_sb);
171173
boost::serialization::throw_exception(
172-
archive_exception(archive_exception::input_stream_error)
174+
archive_exception(
175+
archive_exception::input_stream_error,
176+
message.c_str()
177+
)
173178
);
179+
}
174180
// note: an optimizer should eliminate the following for char files
175181
BOOST_ASSERT(count % sizeof(Elem) <= boost::integer_traits<std::streamsize>::const_max);
176182
s = static_cast<std::streamsize>(count % sizeof(Elem));
@@ -181,10 +187,16 @@ basic_binary_iprimitive<Archive, Elem, Tr>::load_binary(
181187
// );
182188
Elem t;
183189
scount = m_sb.sgetn(& t, 1);
184-
if(scount != 1)
190+
if(scount != 1){
191+
const std::string message =
192+
detail::stream_position_message(& m_sb);
185193
boost::serialization::throw_exception(
186-
archive_exception(archive_exception::input_stream_error)
194+
archive_exception(
195+
archive_exception::input_stream_error,
196+
message.c_str()
197+
)
187198
);
199+
}
188200
std::memcpy(static_cast<char*>(address) + (count - s), &t, static_cast<std::size_t>(s));
189201
}
190202
}

‎include/boost/archive/basic_text_iprimitive.hpp‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
// use two template parameters
2626

2727
#include <locale>
28+
#include <string>
2829
#include <cstddef> // size_t
2930

3031
#include <boost/config.hpp>
@@ -48,6 +49,7 @@ namespace std{
4849
#include <boost/archive/codecvt_null.hpp>
4950
#include <boost/archive/archive_exception.hpp>
5051
#include <boost/archive/basic_streambuf_locale_saver.hpp>
52+
#include <boost/archive/detail/stream_position_message.hpp>
5153
#include <boost/archive/detail/abi_prefix.hpp> // must be the last header
5254

5355
namespace boost {
@@ -88,8 +90,13 @@ class BOOST_SYMBOL_VISIBLE basic_text_iprimitive {
8890
{
8991
if(is >> t)
9092
return;
93+
const std::string message =
94+
detail::stream_position_message(is.rdbuf());
9195
boost::serialization::throw_exception(
92-
archive_exception(archive_exception::input_stream_error)
96+
archive_exception(
97+
archive_exception::input_stream_error,
98+
message.c_str()
99+
)
93100
);
94101
}
95102

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#ifndef BOOST_ARCHIVE_DETAIL_STREAM_POSITION_MESSAGE_HPP
2+
#define BOOST_ARCHIVE_DETAIL_STREAM_POSITION_MESSAGE_HPP
3+
4+
// MS compatible compilers support #pragma once
5+
#if defined(_MSC_VER)
6+
# pragma once
7+
#endif
8+
9+
/////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
10+
// stream_position_message.hpp: a string describing where an input stream
11+
// error was met
12+
13+
// Copyright 2026 Gennaro Prota.
14+
// Distributed under the Boost Software License, Version 1.0.
15+
// (See accompanying file LICENSE_1_0.txt or copy at
16+
// http://www.boost.org/LICENSE_1_0.txt)
17+
18+
// See http://www.boost.org for updates, documentation, and revision history.
19+
20+
#include <cstddef>
21+
#include <ios>
22+
#include <streambuf>
23+
#include <string>
24+
25+
namespace boost {
26+
namespace archive {
27+
namespace detail {
28+
29+
// The buffer is asked, rather than the stream it belongs to, because a
30+
// stream in a fail state answers -1 whatever its position really is. A
31+
// buffer which is absent, or which cannot seek, answers -1 as well, and is
32+
// reported as an unknown position.
33+
template<class Elem, class Tr>
34+
std::string stream_position_message(
35+
std::basic_streambuf<Elem, Tr> * const sb
36+
){
37+
const std::streampos pos = (NULL == sb)
38+
? std::streampos(-1)
39+
: sb->pubseekoff(0, std::ios_base::cur, std::ios_base::in);
40+
return "at offset "
41+
+ (std::streampos(-1) == pos
42+
? std::string("<unknown>")
43+
: std::to_string(static_cast<std::streamoff>(pos)));
44+
}
45+
46+
}
47+
}
48+
}
49+
50+
#endif // BOOST_ARCHIVE_DETAIL_STREAM_POSITION_MESSAGE_HPP

‎include/boost/archive/impl/basic_xml_iarchive.ipp‎

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <boost/serialization/throw_exception.hpp>
1717
#include <boost/archive/xml_archive_exception.hpp>
1818
#include <boost/archive/basic_xml_iarchive.hpp>
19+
#include <boost/archive/detail/stream_position_message.hpp>
1920
#include <boost/serialization/tracking.hpp>
2021

2122
namespace boost {
@@ -32,8 +33,14 @@ basic_xml_iarchive<Archive>::load_start(const char *name){
3233
return;
3334
bool result = this->This()->gimpl->parse_start_tag(this->This()->get_is());
3435
if(true != result){
36+
const std::string message = detail::stream_position_message(
37+
this->This()->get_is().rdbuf()
38+
);
3539
boost::serialization::throw_exception(
36-
archive_exception(archive_exception::input_stream_error)
40+
archive_exception(
41+
archive_exception::input_stream_error,
42+
message.c_str()
43+
)
3744
);
3845
}
3946
// don't check start tag at highest level
@@ -48,8 +55,14 @@ basic_xml_iarchive<Archive>::load_end(const char *name){
4855
return;
4956
bool result = this->This()->gimpl->parse_end_tag(this->This()->get_is());
5057
if(true != result){
58+
const std::string message = detail::stream_position_message(
59+
this->This()->get_is().rdbuf()
60+
);
5161
boost::serialization::throw_exception(
52-
archive_exception(archive_exception::input_stream_error)
62+
archive_exception(
63+
archive_exception::input_stream_error,
64+
message.c_str()
65+
)
5366
);
5467
}
5568

‎test/Jamfile.v2‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ if ! $(BOOST_ARCHIVE_LIST) {
160160

161161
[ test-bsl-run test_duplicate_type_registration ]
162162
[ test-bsl-run test_strong_typedef_move ]
163+
[ test-bsl-run test_stream_error_position ]
163164
[ test-bsl-run test_private_ctor ]
164165
[ test-bsl-run test_reset_object_address : A ]
165166
[ test-bsl-run test_void_cast ]
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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

Comments
 (0)