Skip to content

[scudo] Change isPowerOfTwo macro to return false for zero. #87120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion compiler-rt/lib/scudo/standalone/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ template <class Dest, class Source> inline Dest bit_cast(const Source &S) {
return D;
}

inline constexpr bool isPowerOfTwo(uptr X) { return (X & (X - 1)) == 0; }
inline constexpr bool isPowerOfTwo(uptr X) {
if (X == 0)
return false;
return (X & (X - 1)) == 0;
}

inline constexpr uptr roundUp(uptr X, uptr Boundary) {
DCHECK(isPowerOfTwo(Boundary));
Expand Down
4 changes: 2 additions & 2 deletions compiler-rt/lib/scudo/standalone/stack_depot.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class alignas(atomic_u64) StackDepot {
// Ensure that RingSize, RingMask and TabMask are set up in a way that
// all accesses are within range of BufSize.
bool isValid(uptr BufSize) const {
if (RingSize == 0 || !isPowerOfTwo(RingSize))
if (!isPowerOfTwo(RingSize))
return false;
uptr RingBytes = sizeof(atomic_u64) * RingSize;
if (RingMask + 1 != RingSize)
Expand All @@ -112,7 +112,7 @@ class alignas(atomic_u64) StackDepot {
if (TabMask == 0)
return false;
uptr TabSize = TabMask + 1;
if (TabSize == 0 || !isPowerOfTwo(TabSize))
if (!isPowerOfTwo(TabSize))
return false;
uptr TabBytes = sizeof(atomic_u32) * TabSize;

Expand Down
6 changes: 2 additions & 4 deletions compiler-rt/lib/scudo/standalone/wrappers_c_checks.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,13 @@ inline void *setErrnoOnNull(void *Ptr) {
// Checks aligned_alloc() parameters, verifies that the alignment is a power of
// two and that the size is a multiple of alignment.
inline bool checkAlignedAllocAlignmentAndSize(uptr Alignment, uptr Size) {
return Alignment == 0 || !isPowerOfTwo(Alignment) ||
!isAligned(Size, Alignment);
return !isPowerOfTwo(Alignment) || !isAligned(Size, Alignment);
}

// Checks posix_memalign() parameters, verifies that alignment is a power of two
// and a multiple of sizeof(void *).
inline bool checkPosixMemalignAlignment(uptr Alignment) {
return Alignment == 0 || !isPowerOfTwo(Alignment) ||
!isAligned(Alignment, sizeof(void *));
return !isPowerOfTwo(Alignment) || !isAligned(Alignment, sizeof(void *));
}

// Returns true if calloc(Size, N) overflows on Size*N calculation. Use a
Expand Down