diff options
author | Christopher Ferris <cferris1000@users.noreply.github.com> | 2024-04-01 13:35:29 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2024-04-01 13:58:24 -0700 |
commit | 9e8419f58721f02bdc7db58ca98b2e7c17880f8d (patch) | |
tree | 9cd490c4cfc96a8a459928e8350ad1bfde6ddea9 | |
parent | b2e25a8717a93495a3b7345786b24b1402a75479 (diff) | |
download | scudo-9e8419f58721f02bdc7db58ca98b2e7c17880f8d.tar.gz |
[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.
GitOrigin-RevId: ed6edf262d9061ce3c024754c4981299b5184ee2
Change-Id: Ide422858b9e766e418d2d2e781c39a092b5562fc
-rw-r--r-- | standalone/common.h | 6 | ||||
-rw-r--r-- | standalone/stack_depot.h | 4 | ||||
-rw-r--r-- | standalone/wrappers_c_checks.h | 6 |
3 files changed, 9 insertions, 7 deletions
diff --git a/standalone/common.h b/standalone/common.h index ae45683f1ee..151fbd317e7 100644 --- a/standalone/common.h +++ b/standalone/common.h @@ -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)); diff --git a/standalone/stack_depot.h b/standalone/stack_depot.h index 98cd9707a64..0176c40aa89 100644 --- a/standalone/stack_depot.h +++ b/standalone/stack_depot.h @@ -103,7 +103,7 @@ public: // 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) @@ -112,7 +112,7 @@ public: 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; diff --git a/standalone/wrappers_c_checks.h b/standalone/wrappers_c_checks.h index 9cd48e82792..d0288699cf1 100644 --- a/standalone/wrappers_c_checks.h +++ b/standalone/wrappers_c_checks.h @@ -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 |