Repository navigation
Commit 7ec98ef
authored
Fix: Prevent out-of-bounds read in mi_ctz_generic32 and mi_clz_generic32
This patch ensures that both mi_ctz_generic32 and mi_clz_generic32 perform safe indexing into the de Bruijn lookup tables by masking the computed index with `& 31`.
On platforms where unsigned long is 64-bit, the result of the de Bruijn multiplication and shift could exceed the valid index range (0–31), leading to an out-of-bounds read.
This change applies a bitwise AND mask to the final index:
- `mi_ctz_generic32`: debruijn[(((x & -(int32_t)x) * 0x077CB531U) >> 27) & 31]
- `mi_clz_generic32`: debruijn[((x * 0x07C4ACDDU) >> 27) & 31]
This matches the fix applied in python/cpython#134070 to its integrated mimalloc copy.
Fixes: python/cpython#1340701 parent af21001 commit 7ec98ef
1 file changed
Lines changed: 4 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
976 | 976 | | |
977 | 977 | | |
978 | 978 | | |
979 | | - | |
| 979 | + | |
| 980 | + | |
980 | 981 | | |
981 | 982 | | |
982 | 983 | | |
| |||
991 | 992 | | |
992 | 993 | | |
993 | 994 | | |
994 | | - | |
| 995 | + | |
| 996 | + | |
995 | 997 | | |
996 | 998 | | |
997 | 999 | | |
| |||
0 commit comments