Skip to content

Commit

Permalink
Merge pull request #243 from Flamefire/off-by-one
Browse files Browse the repository at this point in the history
Fix off-by-one error in parser buffer size
  • Loading branch information
mborland authored Jan 2, 2025
2 parents 8fbdb8a + 73eb4f8 commit 26359d7
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
2 changes: 1 addition & 1 deletion include/boost/charconv/detail/parser.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ inline from_chars_result parser(const char* first, const char* last, bool& sign,
}

// Next we get the significand
constexpr std::size_t significand_buffer_size = limits<Unsigned_Integer>::max_chars10 - 1; // Base 10 or 16
constexpr std::size_t significand_buffer_size = limits<Unsigned_Integer>::max_chars10; // Base 10 or 16
char significand_buffer[significand_buffer_size] {};
std::size_t i = 0;
std::size_t dot_position = 0;
Expand Down
28 changes: 28 additions & 0 deletions test/test_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

#include <boost/charconv/detail/parser.hpp>
#include <boost/charconv/chars_format.hpp>
#include <boost/charconv/to_chars.hpp>
#include <boost/core/lightweight_test.hpp>
#include <system_error>
#include <type_traits>
Expand Down Expand Up @@ -38,6 +39,33 @@ void test_integer()

auto r3 = boost::charconv::detail::parser(val2, val2 + std::strlen(val2), sign, significand, exponent, boost::charconv::chars_format::scientific);
BOOST_TEST(r3.ec == std::errc::invalid_argument);

// Get the maximum value of the significant type
constexpr auto max_sig_v = std::numeric_limits<decltype(significand)>::max();
char max_sig_buf[boost::charconv::limits<decltype(significand)>::max_chars10 + 1u];
const auto r4 = boost::charconv::to_chars(max_sig_buf + 1, max_sig_buf + sizeof(max_sig_buf), max_sig_v);
if (BOOST_TEST(r4)) {

significand = 0;
exponent = 1;
sign = true;

auto r5 = boost::charconv::detail::parser(max_sig_buf + 1, r4.ptr, sign, significand, exponent);
BOOST_TEST(r5);
BOOST_TEST_EQ(sign, false);
BOOST_TEST_EQ(exponent, 0);
BOOST_TEST_EQ(significand, max_sig_v);

significand = 0;
exponent = 1;
sign = false;
max_sig_buf[0] = '-';
auto r6 = boost::charconv::detail::parser(max_sig_buf, r4.ptr, sign, significand, exponent);
BOOST_TEST(r6);
BOOST_TEST_EQ(sign, true);
BOOST_TEST_EQ(exponent, 0);
BOOST_TEST_EQ(significand, max_sig_v);
}
}

template <typename T>
Expand Down

0 comments on commit 26359d7

Please sign in to comment.