Skip to content

Commit

Permalink
fix: avoid warnings on MSVC (#12697)
Browse files Browse the repository at this point in the history
Warnings in header files can be a problem for consumers that enable `/WX` (or `-Werror`).  In this case, using `... & -align` produces a warning (C4146) with MSVC. The fix is to use equivalent expression `...  & ~(align - 1)`, which was already used in the same file.

Fixes #12675

Closes #12697

COPYBARA_INTEGRATE_REVIEW=#12697 from coryan:fix-msvc-warnings-in-arena-align 835f3b4
PiperOrigin-RevId: 530137165
  • Loading branch information
coryan authored and fowles committed May 7, 2023
1 parent fe1277f commit b880933
Showing 1 changed file with 6 additions and 4 deletions.
10 changes: 6 additions & 4 deletions src/google/protobuf/arena_align.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ struct ArenaAlignDefault {
}

static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Ceil(size_t n) {
return (n + align - 1) & -align;
return (n + align - 1) & ~(align - 1);
}
static inline PROTOBUF_ALWAYS_INLINE constexpr size_t Floor(size_t n) {
return (n & ~(align - 1));
Expand All @@ -113,7 +113,7 @@ struct ArenaAlignDefault {
template <typename T>
static inline PROTOBUF_ALWAYS_INLINE T* Ceil(T* ptr) {
uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
return reinterpret_cast<T*>((intptr + align - 1) & -align);
return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
}

template <typename T>
Expand Down Expand Up @@ -142,7 +142,9 @@ struct ArenaAlign {
return (reinterpret_cast<uintptr_t>(ptr) & (align - 1)) == 0U;
}

constexpr size_t Ceil(size_t n) const { return (n + align - 1) & -align; }
constexpr size_t Ceil(size_t n) const {
return (n + align - 1) & ~(align - 1);
}
constexpr size_t Floor(size_t n) const { return (n & ~(align - 1)); }

constexpr size_t Padded(size_t n) const {
Expand All @@ -156,7 +158,7 @@ struct ArenaAlign {
template <typename T>
T* Ceil(T* ptr) const {
uintptr_t intptr = reinterpret_cast<uintptr_t>(ptr);
return reinterpret_cast<T*>((intptr + align - 1) & -align);
return reinterpret_cast<T*>((intptr + align - 1) & ~(align - 1));
}

template <typename T>
Expand Down

0 comments on commit b880933

Please sign in to comment.