Skip to content

Commit 0d83927

Browse files
committed
Made basicUnsafeNew for primitive vectors check for overflows
1 parent 59c7750 commit 0d83927

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

Data/Vector/Primitive/Mutable.hs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,13 @@ instance Prim a => G.MVector MVector a where
9090
between x y z = x >= y && x < z
9191

9292
{-# INLINE basicUnsafeNew #-}
93-
basicUnsafeNew n = MVector 0 n
94-
`liftM` newByteArray (n * sizeOf (undefined :: a))
93+
basicUnsafeNew n
94+
| n < 0 = error $ "Primitive.basicUnsafeNew: negative length: " ++ show n
95+
| n > mx = error $ "Primitive.basicUnsafeNew: length to large: " ++ show n
96+
| otherwise = MVector 0 n `liftM` newByteArray (n * size)
97+
where
98+
size = sizeOf (undefined :: a)
99+
mx = maxBound `div` size :: Int
95100

96101
{-# INLINE basicUnsafeRead #-}
97102
basicUnsafeRead (MVector i _ arr) j = readByteArray arr (i+j)

0 commit comments

Comments
 (0)