@@ -65,6 +65,8 @@ import Streamly.Internal.Data.Fold.Type (Fold(..), Step(..))
65import Streamly.Internal.Data.Tuple.Strict
65import Streamly.Internal.Data.Tuple.Strict
66 (Tuple'(..), Tuple3Fused' (Tuple3Fused'))
66 (Tuple'(..), Tuple3Fused' (Tuple3Fused'))
68+import Foreign.ForeignPtr.Unsafe (unsafeForeignPtrToPtr)
68import qualified Streamly.Internal.Data.Fold.Type as Fold
70import qualified Streamly.Internal.Data.Fold.Type as Fold
69import qualified Streamly.Internal.Data.Ring as Ring
71import qualified Streamly.Internal.Data.Ring as Ring
@@ -300,7 +302,11 @@ windowRange n = Fold step initial extract extract
300 if i == 0
302 if i == 0
301 then return Nothing
303 then return Nothing
303− x <- liftIO $ peek rh
305+ -- Here we use "ringStart" over "ringHead" as "ringHead" will be
306+ -- uninitialized if the ring is not full.
307+ -- Using "unsafeForeignPtrToPtr" here is safe as we touch the ring
308+ -- again in "foldFunc".
309+ x <- liftIO $ peek (unsafeForeignPtrToPtr (Ring.ringStart rb))
304 let accum (mn, mx) a = return (min mn a, max mx a)
310 let accum (mn, mx) a = return (min mn a, max mx a)
305 fmap Just $ foldFunc i rh accum (x, x) rb
311 fmap Just $ foldFunc i rh accum (x, x) rb