Skip to content

Commit 7d180aa

Browse files
committed
Merge pull request #85 from dolio/master
Avoid overflow on creation of storable vectors.
2 parents 1eb5637 + dd2c1e8 commit 7d180aa

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

Data/Vector/Storable/Mutable.hs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,10 +117,14 @@ instance Storable a => G.MVector MVector a where
117117

118118
{-# INLINE basicUnsafeNew #-}
119119
basicUnsafeNew n
120-
= unsafePrimToPrim
121-
$ do
120+
| n < 0 = error $ "Storable.basicUnsafeNew: negative length: " ++ show n
121+
| n > mx = error $ "Storable.basicUnsafeNew: length too large: " ++ show n
122+
| otherwise = unsafePrimToPrim $ do
122123
fp <- mallocVector n
123124
return $ MVector n fp
125+
where
126+
size = sizeOf (undefined :: a)
127+
mx = maxBound `quot` size :: Int
124128

125129
{-# INLINE basicInitialize #-}
126130
basicInitialize = storableZero

0 commit comments

Comments
 (0)