Skip to content

Commit e8466b0

Browse files
authored
Fix: Prevent out-of-bounds read in mi_clz32 and mi_ctz32 (python#134070)
1 parent 49ea8a0 commit e8466b0

File tree

1 file changed

+4
-2
lines changed

1 file changed

+4
-2
lines changed

Include/internal/mimalloc/mimalloc/internal.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,8 @@ static inline size_t mi_ctz32(uint32_t x) {
851851
31, 27, 13, 23, 21, 19, 16, 7, 26, 12, 18, 6, 11, 5, 10, 9
852852
};
853853
if (x==0) return 32;
854-
return debruijn[((x & -(int32_t)x) * 0x077CB531UL) >> 27];
854+
return debruijn[(uint32_t)(((x & -(int32_t)x) * 0x077CB531UL) >> 27) & 31];
855+
855856
}
856857
static inline size_t mi_clz32(uint32_t x) {
857858
// de Bruijn multiplication, see <http://supertech.csail.mit.edu/papers/debruijn.pdf>
@@ -865,7 +866,8 @@ static inline size_t mi_clz32(uint32_t x) {
865866
x |= x >> 4;
866867
x |= x >> 8;
867868
x |= x >> 16;
868-
return debruijn[(uint32_t)(x * 0x07C4ACDDUL) >> 27];
869+
return debruijn[(uint32_t)(x * 0x07C4ACDDUL >> 27) & 31];
870+
869871
}
870872

871873
static inline size_t mi_clz(uintptr_t x) {

0 commit comments

Comments
 (0)