From 30ec7144671f10b0e18ba1f657dec892629909c6 Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Thu, 14 Apr 2022 11:50:26 +0200 Subject: [PATCH 1/2] Fix UB on 32 bit platforms --- Data/HashMap/Internal.hs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Data/HashMap/Internal.hs b/Data/HashMap/Internal.hs index 2ffeacaa..cc58917a 100644 --- a/Data/HashMap/Internal.hs +++ b/Data/HashMap/Internal.hs @@ -144,7 +144,7 @@ import Control.DeepSeq (NFData (..), NFData1 (..), NFData2 (..)) import Control.Monad.ST (ST, runST) import Data.Bifoldable (Bifoldable (..)) import Data.Bits (complement, popCount, unsafeShiftL, - unsafeShiftR, (.&.), (.|.), countTrailingZeros) + unsafeShiftR, (.&.), (.|.), countTrailingZeros, shiftL) import Data.Coerce (coerce) import Data.Data (Constr, Data (..), DataType) import Data.Functor.Classes (Eq1 (..), Eq2 (..), Ord1 (..), Ord2 (..), @@ -2257,7 +2257,7 @@ index w s = fromIntegral $ unsafeShiftR w s .&. subkeyMask -- | A bitmask with the 'bitsPerSubkey' least significant bits set. fullNodeMask :: Bitmap -fullNodeMask = complement (complement 0 `unsafeShiftL` maxChildren) +fullNodeMask = complement (complement 0 `shiftL` maxChildren) {-# INLINE fullNodeMask #-} -- | Check if two the two arguments are the same value. N.B. This From 909be5ef03252da86206f24cdd27db6b653dfbf7 Mon Sep 17 00:00:00 2001 From: konsumlamm Date: Thu, 14 Apr 2022 12:33:24 +0200 Subject: [PATCH 2/2] Add comment about UB --- Data/HashMap/Internal.hs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/Data/HashMap/Internal.hs b/Data/HashMap/Internal.hs index cc58917a..c5912a7a 100644 --- a/Data/HashMap/Internal.hs +++ b/Data/HashMap/Internal.hs @@ -2257,6 +2257,8 @@ index w s = fromIntegral $ unsafeShiftR w s .&. subkeyMask -- | A bitmask with the 'bitsPerSubkey' least significant bits set. fullNodeMask :: Bitmap +-- This needs to use 'shiftL' instead of 'unsafeShiftL', to avoid UB. +-- See issue #412. fullNodeMask = complement (complement 0 `shiftL` maxChildren) {-# INLINE fullNodeMask #-}