Skip to content

Commit f1da16f

Browse files
authored
fix runtime array bound checking (#255)
1 parent 5e5f2a0 commit f1da16f

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

runtime/src/Language/Granule/Runtime.hs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module Language.Granule.Runtime
2424
-- Re-exported from Prelude
2525
, String, Int, IO, Float, Maybe(..), Show(..), Char, getLine
2626
, putStr, read, (<$>) , fromIntegral, Monad(..)
27-
, ($), error, (>), (++), id, Num(..), (.)
27+
, ($), error, (>=), (++), id, Num(..), (.)
2828
, pack, Text
2929
) where
3030

@@ -39,7 +39,7 @@ import Criterion.Main ( defaultMain, bench, bgroup, nfAppIO )
3939
import System.IO.Silently ( silence )
4040
import Prelude
4141
( Int, IO, Double, Maybe(..), Show(..), Char, read, fromEnum, toEnum
42-
, (<$>), (<>), fromIntegral, ($), error, (>), (++), id, Num (..), (.) )
42+
, (<$>), (<>), fromIntegral, ($), error, (>=), (++), id, Num (..), (.) )
4343
import Control.Monad
4444
import GHC.Err (undefined)
4545
import Data.Function (const)
@@ -183,8 +183,8 @@ writeFloatArray a i v = unsafePerformIO $ writeFloatArraySafe a i v
183183

184184
writeFloatArraySafe :: FloatArray -> Int -> Float -> IO FloatArray
185185
writeFloatArraySafe a i v =
186-
if i > grLength a
187-
then error $ "array index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
186+
if i >= grLength a
187+
then error $ "array index out of bounds: " ++ show i ++ " >= " ++ show (grLength a)
188188
else case a of
189189
HaskellArray{} -> error "expected unique array"
190190
PointerArray len ptr -> do
@@ -197,8 +197,8 @@ writeFloatArrayI a i v = unsafePerformIO $ writeFloatArrayISafe a i v
197197

198198
writeFloatArrayISafe :: FloatArray -> Int -> Float -> IO FloatArray
199199
writeFloatArrayISafe a i v =
200-
if i > grLength a
201-
then error $ "array index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
200+
if i >= grLength a
201+
then error $ "array index out of bounds: " ++ show i ++ " >= " ++ show (grLength a)
202202
else case a of
203203
PointerArray{} -> error "expected non-unique array"
204204
HaskellArray len arr -> do
@@ -231,8 +231,8 @@ readFloatArray a i = unsafePerformIO $ readFloatArraySafe a i
231231

232232
readFloatArraySafe :: FloatArray -> Int -> IO (Float, FloatArray)
233233
readFloatArraySafe a i =
234-
if i > grLength a
235-
then error $ "readFloatArray index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
234+
if i >= grLength a
235+
then error $ "readFloatArray index out of bounds: " ++ show i ++ " >= " ++ show (grLength a)
236236
else case a of
237237
HaskellArray{} -> error "expected unique array"
238238
PointerArray len ptr -> do
@@ -245,8 +245,8 @@ readFloatArrayI a i = unsafePerformIO $ readFloatArrayISafe a i
245245

246246
readFloatArrayISafe :: FloatArray -> Int -> IO (Float, FloatArray)
247247
readFloatArrayISafe a i =
248-
if i > grLength a
249-
then error $ "readFloatArrayI index out of bounds: " ++ show i ++ " > " ++ show (grLength a)
248+
if i >= grLength a
249+
then error $ "readFloatArrayI index out of bounds: " ++ show i ++ " >= " ++ show (grLength a)
250250
else case a of
251251
PointerArray{} -> error "expected non-unique array"
252252
HaskellArray _ arr -> do
@@ -360,4 +360,4 @@ type instance CapabilityType 'TimeDateTag = () -> Text
360360
{-# NOINLINE cap #-}
361361
cap :: Capability cap -> () -> CapabilityType cap
362362
cap Console () = \x -> unsafePerformIO $ toStdout $ x
363-
cap TimeDate () = \() -> unsafePerformIO $ timeDate ()
363+
cap TimeDate () = \() -> unsafePerformIO $ timeDate ()

0 commit comments

Comments
 (0)