Skip to content

Commit

Permalink
Fix calculation of max buffer length not accounting for negative signs
Browse files Browse the repository at this point in the history
  • Loading branch information
mborland committed Apr 18, 2024
1 parent cfbf18f commit 2ba5773
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 4 deletions.
Binary file not shown.
Binary file not shown.
9 changes: 5 additions & 4 deletions include/boost/charconv/detail/to_chars_integer_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char

char buffer[10] {};
int converted_value_digits {};
const std::ptrdiff_t user_buffer_size = last - first;
BOOST_ATTRIBUTE_UNUSED bool is_negative = false;
bool is_negative = false;

if (first > last)
{
Expand All @@ -115,6 +114,8 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
unsigned_value = static_cast<Unsigned_Integer>(value);
}

const std::ptrdiff_t user_buffer_size = last - first - static_cast<std::ptrdiff_t>(is_negative);

// If the type is less than 32 bits we can use this without change
// If the type is greater than 32 bits we use a binary search tree to figure out how many digits
// are present and then decompose the value into two (or more) std::uint32_t of known length so that we
Expand Down Expand Up @@ -314,8 +315,6 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_128integer_impl(char* first, c
template <typename Integer, typename Unsigned_Integer>
BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char* last, Integer value, int base) noexcept
{
const std::ptrdiff_t output_length = last - first;

if (!((first <= last) && (base >= 2 && base <= 36)))
{
return {last, std::errc::invalid_argument};
Expand Down Expand Up @@ -347,6 +346,8 @@ BOOST_CHARCONV_CONSTEXPR to_chars_result to_chars_integer_impl(char* first, char
unsigned_value = static_cast<Unsigned_Integer>(value);
}

const std::ptrdiff_t output_length = last - first;

constexpr Unsigned_Integer zero = 48U; // Char for '0'
constexpr auto buffer_size = sizeof(Unsigned_Integer) * CHAR_BIT;
char buffer[buffer_size] {};
Expand Down

0 comments on commit 2ba5773

Please sign in to comment.