From 6e110fe4193472c2e47aa0563362f95dd8bca37b Mon Sep 17 00:00:00 2001
From: Ben Gamari <ben@smart-cactus.org>
Date: Mon, 26 Dec 2016 13:13:26 -0500
Subject: [PATCH] Fix overflow check

As akio points out, the fix to #229 which I authored previously was blatantly
wrong.
---
 Data/Array/Base.hs | 8 +++++---
 1 file changed, 5 insertions(+), 3 deletions(-)

diff --git a/Data/Array/Base.hs b/Data/Array/Base.hs
index c88e2728..43963ebe 100644
--- a/Data/Array/Base.hs
+++ b/Data/Array/Base.hs
@@ -32,7 +32,7 @@ import GHC.Arr          ( STArray )
 import qualified GHC.Arr as Arr
 import qualified GHC.Arr as ArrST
 import GHC.ST           ( ST(..), runST )
-import GHC.Base         ( IO(..) )
+import GHC.Base         ( IO(..), divInt# )
 import GHC.Exts
 import GHC.Ptr          ( nullPtr, nullFunPtr )
 import GHC.Stable       ( StablePtr(..) )
@@ -1370,10 +1370,12 @@ fLOAT_SCALE  n# = safe_scale scale# n# where !(I# scale#) = SIZEOF_HSFLOAT
 
 safe_scale :: Int# -> Int# -> Int#
 safe_scale scale# n#
-  | isTrue# (res# >=# n#) = res#
-  | otherwise = error "Data.Array.Base.safe_scale: Overflow"
+  | not overflow = res#
+  | otherwise    = error "Data.Array.Base.safe_scale: Overflow"
   where
     !res# = scale# *# n#
+    !overflow = isTrue# (maxN# `divInt#` scale# <# n#)
+    !(I# maxN#) = maxBound
 
 
 bOOL_INDEX :: Int# -> Int#
-- 
GitLab