Skip to content

Commit ed6edf2

Browse files
authored
[scudo] Change isPowerOfTwo macro to return false for zero. (#87120)
Clean-up all of the calls and remove the redundant == 0 checks. There is only one small visible change. For non-Android, the memalign function will now fail if alignment is zero. Before this would have passed.
1 parent ee99475 commit ed6edf2

File tree

3 files changed

+9
-7
lines changed

3 files changed

+9
-7
lines changed

compiler-rt/lib/scudo/standalone/common.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,11 @@ template <class Dest, class Source> inline Dest bit_cast(const Source &S) {
2828
return D;
2929
}
3030

31-
inline constexpr bool isPowerOfTwo(uptr X) { return (X & (X - 1)) == 0; }
31+
inline constexpr bool isPowerOfTwo(uptr X) {
32+
if (X == 0)
33+
return false;
34+
return (X & (X - 1)) == 0;
35+
}
3236

3337
inline constexpr uptr roundUp(uptr X, uptr Boundary) {
3438
DCHECK(isPowerOfTwo(Boundary));

compiler-rt/lib/scudo/standalone/stack_depot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ class alignas(atomic_u64) StackDepot {
103103
// Ensure that RingSize, RingMask and TabMask are set up in a way that
104104
// all accesses are within range of BufSize.
105105
bool isValid(uptr BufSize) const {
106-
if (RingSize == 0 || !isPowerOfTwo(RingSize))
106+
if (!isPowerOfTwo(RingSize))
107107
return false;
108108
uptr RingBytes = sizeof(atomic_u64) * RingSize;
109109
if (RingMask + 1 != RingSize)
@@ -112,7 +112,7 @@ class alignas(atomic_u64) StackDepot {
112112
if (TabMask == 0)
113113
return false;
114114
uptr TabSize = TabMask + 1;
115-
if (TabSize == 0 || !isPowerOfTwo(TabSize))
115+
if (!isPowerOfTwo(TabSize))
116116
return false;
117117
uptr TabBytes = sizeof(atomic_u32) * TabSize;
118118

compiler-rt/lib/scudo/standalone/wrappers_c_checks.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,15 +31,13 @@ inline void *setErrnoOnNull(void *Ptr) {
3131
// Checks aligned_alloc() parameters, verifies that the alignment is a power of
3232
// two and that the size is a multiple of alignment.
3333
inline bool checkAlignedAllocAlignmentAndSize(uptr Alignment, uptr Size) {
34-
return Alignment == 0 || !isPowerOfTwo(Alignment) ||
35-
!isAligned(Size, Alignment);
34+
return !isPowerOfTwo(Alignment) || !isAligned(Size, Alignment);
3635
}
3736

3837
// Checks posix_memalign() parameters, verifies that alignment is a power of two
3938
// and a multiple of sizeof(void *).
4039
inline bool checkPosixMemalignAlignment(uptr Alignment) {
41-
return Alignment == 0 || !isPowerOfTwo(Alignment) ||
42-
!isAligned(Alignment, sizeof(void *));
40+
return !isPowerOfTwo(Alignment) || !isAligned(Alignment, sizeof(void *));
4341
}
4442

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

0 commit comments

Comments
 (0)