@@ -22,7 +22,6 @@ module Streamly.Internal.Data.Serialize
23#ifdef DEBUG
23#ifdef DEBUG
24import Control.Exception (assert)
24import Control.Exception (assert)
25−import Streamly.Internal.Data.Unbox (Unbox(..), sizeOfMutableByteArray)
28import Control.Monad (void)
27import Control.Monad (void)
@@ -32,6 +31,7 @@ import Streamly.Internal.Data.Unbox
32 ( MutableByteArray(..)
31 ( MutableByteArray(..)
33 , PinnedState(..)
32 , PinnedState(..)
34+ , sizeOfMutableByteArray
36import Streamly.Internal.Data.Array.Type (Array(..))
36import Streamly.Internal.Data.Array.Type (Array(..))
37import Streamly.Internal.System.IO (unsafeInlineIO)
37import Streamly.Internal.System.IO (unsafeInlineIO)
@@ -114,10 +114,12 @@ class Serialize a where
114-- Instances
114-- Instances
115--------------------------------------------------------------------------------
115--------------------------------------------------------------------------------
117+-- XXX We do not need to pass the end of data offset, we can just check the
118+-- mutable array end here to avoid a crash, and when we return from deserialize
119+-- we can check if the offset returned is beyond the bound or not.
117{-# INLINE checkBounds #-}
120{-# INLINE checkBounds #-}
118checkBounds :: String -> Int -> MutableByteArray -> IO ()
121checkBounds :: String -> Int -> MutableByteArray -> IO ()
119checkBounds _label _off _arr = do
122checkBounds _label _off _arr = do
121 sz <- sizeOfMutableByteArray _arr
123 sz <- sizeOfMutableByteArray _arr
122 if (_off > sz)
124 if (_off > sz)
123 then error
125 then error
@@ -126,9 +128,6 @@ checkBounds _label _off _arr = do
126 ++ show (_off - 1)
128 ++ show (_off - 1)
127 ++ " max valid offset = " ++ show (sz - 1)
129 ++ " max valid offset = " ++ show (sz - 1)
128 else return ()
130 else return ()
133{-# INLINE deserializeUnsafe #-}
132{-# INLINE deserializeUnsafe #-}
134deserializeUnsafe :: forall a. Unbox a => Int -> MutableByteArray -> IO (Int, a)
133deserializeUnsafe :: forall a. Unbox a => Int -> MutableByteArray -> IO (Int, a)
@@ -143,7 +142,9 @@ serializeUnsafe :: forall a. Unbox a => Int -> MutableByteArray -> a -> IO Int
143serializeUnsafe off arr val =
142serializeUnsafe off arr val =
144 let next = off + Unbox.sizeOf (Proxy :: Proxy a)
143 let next = off + Unbox.sizeOf (Proxy :: Proxy a)
146 checkBounds "serialize" next arr
146 checkBounds "serialize" next arr
147 Unbox.pokeByteIndex off arr val
148 Unbox.pokeByteIndex off arr val
148 pure next
149 pure next