From 4aaffe3391b829296ac6e9954d8072ae79d6fb45 Mon Sep 17 00:00:00 2001 From: Li-yao Xia Date: Mon, 16 Jun 2025 23:09:49 +0200 Subject: [PATCH] Fix stimes for strict text when size wraps around Int --- src/Data/Text.hs | 2 +- tests/Tests/Regressions.hs | 12 +++++++++++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/Data/Text.hs b/src/Data/Text.hs index 21458522..0fb19c40 100644 --- a/src/Data/Text.hs +++ b/src/Data/Text.hs @@ -380,7 +380,7 @@ instance Semigroup Text where | howManyTimes < 0 = P.error "Data.Text.stimes: given number is negative!" | otherwise = let howManyTimesInt = P.fromIntegral howManyTimes :: Int - in if P.fromIntegral howManyTimesInt == howManyTimes + in if P.fromIntegral howManyTimesInt == howManyTimes && howManyTimesInt >= 0 then replicate howManyTimesInt else P.error "Data.Text.stimes: given number does not fit into an Int!" diff --git a/tests/Tests/Regressions.hs b/tests/Tests/Regressions.hs index 60298c0d..97f8f860 100644 --- a/tests/Tests/Regressions.hs +++ b/tests/Tests/Regressions.hs @@ -10,7 +10,7 @@ module Tests.Regressions tests ) where -import Control.Exception (SomeException, handle) +import Control.Exception (ErrorCall, SomeException, handle, evaluate) import Data.Char (isLetter, chr) import GHC.Exts (Int(..), sizeofByteArray#) import System.IO @@ -18,6 +18,7 @@ import Test.Tasty.HUnit (assertBool, assertEqual, assertFailure) import qualified Data.ByteString as B import Data.ByteString.Char8 () import qualified Data.ByteString.Lazy as LB +import Data.Semigroup (stimes) import qualified Data.Text as T import qualified Data.Text.Array as TA import qualified Data.Text.Encoding as TE @@ -183,6 +184,14 @@ t559 = do T.filter undefined (T.filter (const False) "a") @?= "" LT.filter undefined (LT.filter (const False) "a") @?= "" +-- Github #633 +-- stimes checked for an `a` to `Int` to `a` roundtrip, but the `a` and `Int` values could represent different integers. +t633 :: IO () +t633 = + handle (\(_ :: ErrorCall) -> return ()) $ do + _ <- evaluate (stimes (maxBound :: Word) "a" :: T.Text) + assertFailure "should fail" + tests :: F.TestTree tests = F.testGroup "Regressions" [ F.testCase "hGetContents_crash" hGetContents_crash @@ -201,4 +210,5 @@ tests = F.testGroup "Regressions" , F.testCase "t528" t528 , F.testCase "t529" t529 , F.testCase "t559" t559 + , F.testCase "t633" t633 ]