Skip to content

Commit

Permalink
Remove FMT_SAFEBUFFERS (#1966)
Browse files Browse the repository at this point in the history
  • Loading branch information
vitaut committed Oct 30, 2020
1 parent 4081b2f commit 112755c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 21 deletions.
19 changes: 7 additions & 12 deletions include/fmt/format-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -1646,8 +1646,7 @@ struct fixed_handler {
// Implementation of Dragonbox algorithm: https://github.com/jk-jeon/dragonbox.
namespace dragonbox {
// Computes 128-bit result of multiplication of two 64-bit unsigned integers.
FMT_SAFEBUFFERS inline uint128_wrapper umul128(uint64_t x,
uint64_t y) FMT_NOEXCEPT {
inline uint128_wrapper umul128(uint64_t x, uint64_t y) FMT_NOEXCEPT {
#if FMT_USE_INT128
return static_cast<uint128_t>(x) * static_cast<uint128_t>(y);
#elif defined(_MSC_VER) && defined(_M_X64)
Expand Down Expand Up @@ -1675,8 +1674,7 @@ FMT_SAFEBUFFERS inline uint128_wrapper umul128(uint64_t x,
}

// Computes upper 64 bits of multiplication of two 64-bit unsigned integers.
FMT_SAFEBUFFERS inline uint64_t umul128_upper64(uint64_t x,
uint64_t y) FMT_NOEXCEPT {
inline uint64_t umul128_upper64(uint64_t x, uint64_t y) FMT_NOEXCEPT {
#if FMT_USE_INT128
auto p = static_cast<uint128_t>(x) * static_cast<uint128_t>(y);
return static_cast<uint64_t>(p >> 64);
Expand All @@ -1689,8 +1687,7 @@ FMT_SAFEBUFFERS inline uint64_t umul128_upper64(uint64_t x,

// Computes upper 64 bits of multiplication of a 64-bit unsigned integer and a
// 128-bit unsigned integer.
FMT_SAFEBUFFERS inline uint64_t umul192_upper64(uint64_t x, uint128_wrapper y)
FMT_NOEXCEPT {
inline uint64_t umul192_upper64(uint64_t x, uint128_wrapper y) FMT_NOEXCEPT {
uint128_wrapper g0 = umul128(x, y.high());
g0 += umul128_upper64(x, y.low());
return g0.high();
Expand All @@ -1704,8 +1701,7 @@ inline uint32_t umul96_upper32(uint32_t x, uint64_t y) FMT_NOEXCEPT {

// Computes middle 64 bits of multiplication of a 64-bit unsigned integer and a
// 128-bit unsigned integer.
FMT_SAFEBUFFERS inline uint64_t umul192_middle64(uint64_t x, uint128_wrapper y)
FMT_NOEXCEPT {
inline uint64_t umul192_middle64(uint64_t x, uint128_wrapper y) FMT_NOEXCEPT {
uint64_t g01 = x * y.high();
uint64_t g10 = umul128_upper64(x, y.low());
return g01 + g10;
Expand Down Expand Up @@ -2124,8 +2120,8 @@ FMT_ALWAYS_INLINE int remove_trailing_zeros(uint64_t& n) FMT_NOEXCEPT {

// The main algorithm for shorter interval case
template <class T>
FMT_ALWAYS_INLINE FMT_SAFEBUFFERS decimal_fp<T> shorter_interval_case(
int exponent) FMT_NOEXCEPT {
FMT_ALWAYS_INLINE decimal_fp<T> shorter_interval_case(int exponent)
FMT_NOEXCEPT {
decimal_fp<T> ret_value;
// Compute k and beta
const int minus_k = floor_log10_pow2_minus_log10_4_over_3(exponent);
Expand Down Expand Up @@ -2171,8 +2167,7 @@ FMT_ALWAYS_INLINE FMT_SAFEBUFFERS decimal_fp<T> shorter_interval_case(
return ret_value;
}

template <typename T>
FMT_SAFEBUFFERS decimal_fp<T> to_decimal(T x) FMT_NOEXCEPT {
template <typename T> decimal_fp<T> to_decimal(T x) FMT_NOEXCEPT {
// Step 1: integer promotion & Schubfach multiplier calculation.

using carrier_uint = typename float_info<T>::carrier_uint;
Expand Down
13 changes: 4 additions & 9 deletions include/fmt/format.h
Original file line number Diff line number Diff line change
Expand Up @@ -745,8 +745,10 @@ void basic_memory_buffer<T, SIZE, Allocator>::grow(size_t size) {
const size_t max_size = std::allocator_traits<Allocator>::max_size(alloc_);
size_t old_capacity = this->capacity();
size_t new_capacity = old_capacity + old_capacity / 2;
if (size > new_capacity) new_capacity = size;
else if (new_capacity > max_size) new_capacity = (std::max)(size, max_size);
if (size > new_capacity)
new_capacity = size;
else if (new_capacity > max_size)

This comment has been minimized.

Copy link
@bbolli

bbolli Oct 30, 2020

Contributor

Shouldn't we remove this else? With it, a size > new_capacity would skip the check new_capacity > max_size. So if size were greater than max_size, we'd allocate more than allowed.

This comment has been minimized.

Copy link
@vitaut

vitaut Oct 30, 2020

Author Contributor

No. In this allocate will fail. Also this is irrelevant to the current commit.

This comment has been minimized.

Copy link
@bbolli

bbolli Oct 30, 2020

Contributor

Ok, thanks.

new_capacity = (std::max)(size, max_size);
T* old_data = this->data();
T* new_data =
std::allocator_traits<Allocator>::allocate(alloc_, new_capacity);
Expand Down Expand Up @@ -979,13 +981,6 @@ template <> int count_digits<4>(detail::fallback_uintptr n);
# define FMT_ALWAYS_INLINE inline
#endif

// To suppress unnecessary security cookie checks
#if FMT_MSC_VER && !FMT_CLANG_VERSION
# define FMT_SAFEBUFFERS __declspec(safebuffers)
#else
# define FMT_SAFEBUFFERS
#endif

#ifdef FMT_BUILTIN_CLZ
// Optional version of count_digits for better performance on 32-bit platforms.
inline int count_digits(uint32_t n) {
Expand Down

0 comments on commit 112755c

Please sign in to comment.