From 2a688464bf3c7659a6ef08d20812f6f14721056c Mon Sep 17 00:00:00 2001 From: memurro000 Date: Thu, 20 Nov 2025 16:45:04 +0300 Subject: [PATCH] Fix use-of-uninitialized-value in XML parser - Add stream state validation before XML parsing - Check for empty input to prevent uninitialized memory access - Throw appropriate xml_parser_error for invalid inputs Fixes: boostorg/boost#1099 --- .../detail/xml_parser_read_rapidxml.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) 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 9c04219189..26fc540c9b 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()),