From 16904dd77d2871124e73621de95a56b2df741842 Mon Sep 17 00:00:00 2001 From: Adrian Cochrane Date: Fri, 17 Mar 2023 15:45:34 +1300 Subject: [PATCH] Bounds-check exceptions to silence erroneous errors. --- Graphics/Text/Font/Choose/Result.hs | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/Graphics/Text/Font/Choose/Result.hs b/Graphics/Text/Font/Choose/Result.hs index c66e78a..8ddc9b6 100644 --- a/Graphics/Text/Font/Choose/Result.hs +++ b/Graphics/Text/Font/Choose/Result.hs @@ -6,10 +6,14 @@ import Foreign.Ptr (Ptr, nullPtr) import Control.Exception (throwIO, throw, Exception) data Result = Match | NoMatch | TypeMismatch | ResultNoId | OutOfMemory | Other - deriving (Eq, Show, Read, Enum) + deriving (Eq, Show, Read, Enum, Bounded) resultFromPointer :: Ptr Int -> IO Result -resultFromPointer res = toEnum <$> peek res +resultFromPointer res = do + ret <- peek res + if ret > fromEnum (maxBound :: Result) || ret < fromEnum (minBound :: Result) + then return Match -- FIXME: Why these erroneous exceptions thrown? + else return $ toEnum ret data Error = ErrTypeMismatch | ErrResultNoId | ErrOutOfMemory deriving (Eq, Show, Read) instance Exception Error -- 2.30.2