Skip to content

Commit

Permalink
Fix Hasura.Cache.Bounded.mkCacheSize to make sure we don't silently w…
Browse files Browse the repository at this point in the history
…rap or accept 0
  • Loading branch information
jberryman authored and lexi-lambda committed Dec 20, 2019
1 parent 3f848a5 commit 5ca654d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions server/src-lib/Hasura/Cache/Bounded.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,20 @@ import qualified Data.Aeson as J
import qualified Data.HashPSQ as HashPSQ
import qualified Data.IORef as IORef
import qualified Data.Vector as V
import GHC.Natural (Natural)

newtype CacheSize
= CacheSize { unCacheSize :: Word16 }
deriving (Show, Read, Eq, Ord, Bounded, Num, Enum, J.ToJSON, J.FromJSON)

mkCacheSize :: String -> Either String CacheSize
mkCacheSize v =
case readMaybe v of
Just w16 -> return (CacheSize w16)
Nothing -> fail "cache size should be between 1 - 65,535"
-- NOTE: naively using readMaybe Word16 will silently wrap
case readMaybe v :: Maybe Natural of
Just n | n <= max16 && n > 0 -> return (CacheSize $ fromIntegral n)
_ -> fail "cache size must be given as a number between 1 and 65535"
where
max16 = fromIntegral (maxBound :: Word16) :: Natural

newtype Tick
= Tick { unTick :: Word64 }
Expand Down

0 comments on commit 5ca654d

Please sign in to comment.