diff --git a/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp b/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp index 9c0421918..26fc540c9 100644 --- a/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp +++ b/include/boost/property_tree/detail/xml_parser_read_rapidxml.hpp @@ -92,6 +92,18 @@ namespace boost { namespace property_tree { namespace xml_parser typedef typename Ptree::key_type::value_type Ch; using namespace detail::rapidxml; + // Validate that the stream is good + // because peek in the next validation step otherwise could produce UB + if (!stream.good()) + BOOST_PROPERTY_TREE_THROW( + xml_parser_error("bad istream reference", filename, 0)); + + // Validate that the stream is initialized and not empty before reading + // so address sanitizer produces no error + if (stream.peek() == std::char_traits::eof()) + BOOST_PROPERTY_TREE_THROW( + xml_parser_error("uninitialized or empty istream reference", filename, 0)); + // Load data into vector stream.unsetf(std::ios::skipws); std::vector v(std::istreambuf_iterator(stream.rdbuf()),