Skip to content

Commit 5578f3b

Browse files
LysxiaBodigrim
authored andcommitted
Fix stimes for strict text when size wraps around Int
1 parent ddd22b7 commit 5578f3b

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

src/Data/Text.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ instance Semigroup Text where
380380
| howManyTimes < 0 = P.error "Data.Text.stimes: given number is negative!"
381381
| otherwise =
382382
let howManyTimesInt = P.fromIntegral howManyTimes :: Int
383-
in if P.fromIntegral howManyTimesInt == howManyTimes
383+
in if P.fromIntegral howManyTimesInt == howManyTimes && howManyTimesInt >= 0
384384
then replicate howManyTimesInt
385385
else P.error "Data.Text.stimes: given number does not fit into an Int!"
386386

tests/Tests/Regressions.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ module Tests.Regressions
1010
tests
1111
) where
1212

13-
import Control.Exception (SomeException, handle)
13+
import Control.Exception (ErrorCall, SomeException, handle, evaluate)
1414
import Data.Char (isLetter, chr)
1515
import GHC.Exts (Int(..), sizeofByteArray#)
1616
import System.IO
@@ -19,6 +19,7 @@ import Test.Tasty.HUnit (assertBool, assertEqual, assertFailure)
1919
import qualified Data.ByteString as B
2020
import Data.ByteString.Char8 ()
2121
import qualified Data.ByteString.Lazy as LB
22+
import Data.Semigroup (stimes)
2223
import qualified Data.Text as T
2324
import qualified Data.Text.Array as TA
2425
import qualified Data.Text.Encoding as TE
@@ -181,6 +182,14 @@ t559 = do
181182
T.filter undefined (T.filter (const False) "a") @?= ""
182183
LT.filter undefined (LT.filter (const False) "a") @?= ""
183184

185+
-- Github #633
186+
-- stimes checked for an `a` to `Int` to `a` roundtrip, but the `a` and `Int` values could represent different integers.
187+
t633 :: IO ()
188+
t633 =
189+
handle (\(_ :: ErrorCall) -> return ()) $ do
190+
_ <- evaluate (stimes (maxBound :: Word) "a" :: T.Text)
191+
assertFailure "should fail"
192+
184193
tests :: F.TestTree
185194
tests = F.testGroup "Regressions"
186195
[ F.testCase "hGetContents_crash" hGetContents_crash
@@ -199,4 +208,5 @@ tests = F.testGroup "Regressions"
199208
, F.testCase "t528" t528
200209
, F.testCase "t529" t529
201210
, F.testCase "t559" t559
211+
, F.testCase "t633" t633
202212
]

0 commit comments

Comments
 (0)