Commits

Aleksey Khudyakov  committed b97722a

Check for negative number of bins.

  • Participants
  • Parent commits 9622e36

Comments (0)

Files changed (1)

File Data/Histogram/ST.hs

                          , freezeHist
                          ) where
 
+import Control.Monad
 import Control.Monad.Primitive
 
 import qualified Data.Vector.Generic         as G
 newMHistogram :: (PrimMonad m, Bin bin, M.MVector v a) => a -> bin -> m (MHistogram (PrimState m) v bin a)
 newMHistogram zero bin = do
   let n = nBins bin
+  -- NOTE: replicate behaves strangely when n is negative it may fail
+  --       immediately or proceed and create invalid vector which
+  --       leads to memory corruption. So we want to check that.
+  when (n < 0) $
+    error "Data.Histogram.ST.newMHistogram: negative number of bins"
   a  <- M.replicate (n + 2) zero
   return $ MHistogram n bin a
 {-# INLINE newMHistogram #-}