From 50b5b1186819c1a7a63a6fb52055cfe90ba72545 Mon Sep 17 00:00:00 2001 From: Mihai Maruseac Date: Sun, 30 Apr 2017 18:56:21 -0700 Subject: [PATCH 1/2] Solve #167 No more `error: division by zero` when trying to print or operate on empty storable vectors --- Data/Vector/Storable/Mutable.hs | 1 + 1 file changed, 1 insertion(+) diff --git a/Data/Vector/Storable/Mutable.hs b/Data/Vector/Storable/Mutable.hs index 29eb2fbf..ac2f1d28 100644 --- a/Data/Vector/Storable/Mutable.hs +++ b/Data/Vector/Storable/Mutable.hs @@ -123,6 +123,7 @@ instance Storable a => G.MVector MVector a where {-# INLINE basicUnsafeNew #-} basicUnsafeNew n | n < 0 = error $ "Storable.basicUnsafeNew: negative length: " ++ show n + | size < 1 = error $ "Storable.basicUnsafeNew: length of element type too small: " ++ show size | n > mx = error $ "Storable.basicUnsafeNew: length too large: " ++ show n | otherwise = unsafePrimToPrim $ do fp <- mallocVector n From 3e2bd9f610490b1ff694076597ad7de591810dfd Mon Sep 17 00:00:00 2001 From: Mihai Maruseac Date: Sun, 30 Apr 2017 19:55:29 -0700 Subject: [PATCH 2/2] Remove the error, make the division be at least a division by 1 --- Data/Vector/Storable/Mutable.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Data/Vector/Storable/Mutable.hs b/Data/Vector/Storable/Mutable.hs index ac2f1d28..a1cd3f49 100644 --- a/Data/Vector/Storable/Mutable.hs +++ b/Data/Vector/Storable/Mutable.hs @@ -123,13 +123,12 @@ instance Storable a => G.MVector MVector a where {-# INLINE basicUnsafeNew #-} basicUnsafeNew n | n < 0 = error $ "Storable.basicUnsafeNew: negative length: " ++ show n - | size < 1 = error $ "Storable.basicUnsafeNew: length of element type too small: " ++ show size | n > mx = error $ "Storable.basicUnsafeNew: length too large: " ++ show n | otherwise = unsafePrimToPrim $ do fp <- mallocVector n return $ MVector n fp where - size = sizeOf (undefined :: a) + size = sizeOf (undefined :: a) `max` 1 mx = maxBound `quot` size :: Int {-# INLINE basicInitialize #-}