Skip to content

Fix stimes for strict text when size wraps around Int #639

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 21, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/Data/Text.hs
Original file line number Diff line number Diff line change
Expand Up @@ -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!"

Expand Down
12 changes: 11 additions & 1 deletion tests/Tests/Regressions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,15 @@ 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
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
Expand Down Expand Up @@ -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
Expand All @@ -201,4 +210,5 @@ tests = F.testGroup "Regressions"
, F.testCase "t528" t528
, F.testCase "t529" t529
, F.testCase "t559" t559
, F.testCase "t633" t633
]