Opened 5 years ago

Closed 5 years ago

#7052 closed bug (duplicate)

Numeric types’ Read instances use exponential CPU/memory

Reported by: andersk Owned by:
Priority: normal Milestone: 7.6.1
Component: libraries/base Version: 7.4.2
Keywords: Cc:
Operating System: Unknown/Multiple Architecture: Unknown/Multiple
Type of failure: Runtime performance bug Test Case:
Blocked By: Blocking:
Related Tickets: #5688 Differential Rev(s):
Wiki Page:



read "1e99999999"
Int -- returns 0
read "1e99999999"
Integer -- returns [REDACTED]
read "1e99999999"
Float -- returns Infinity
read "1e99999999"
Double -- returns Infinity
takes rather a lot of CPU time and memory, and adding more digits to the exponent makes it take exponentially more. In some applications, this behavior could be exploitable as a denial of service.

I’m not sure what the right solution is for Integer, but clearly all the other cases should be optimized to produce 0 or Infinity very quickly.

Change History (5)

comment:1 Changed 5 years ago by pcapriotti

difficulty: Unknown
Resolution: fixed
Status: newclosed

Are you sure you're using 7.4.2? This should be fixed, and indeed I can't reproduce it on 7.4.2. See #5688.

comment:2 Changed 5 years ago by andersk


My mistake; I’m still on 7.4.1. Thanks.

comment:3 Changed 5 years ago by andersk

Resolution: fixed
Status: closednew

Okay hold on, there’s still something wrong in 7.4.2 (I just downloaded ghc-7.4.2-x86_64-unknown-linux.tar.bz2).

> read ("1e" ++ show (2^63 - 2)) :: Double
Infinity  -- Fine.
> read ("1e" ++ show (2^63 - 1)) :: Double
0.0       -- What?
> read ("1e" ++ show (2^64 - 2^10 - 2)) :: Double
0.0       -- What?
> read ("1e" ++ show (2^64 - 2^10 - 1)) :: Double
          -- Memory-eating infinite loop!

comment:4 Changed 5 years ago by andersk

Also it looks like GHC itself (also 7.4.2) is not using the improved read, as compiling x = 1e99999999 or typing 1e99999999 into GHCi uses lots of CPU and memory for several seconds, although this is of course a less critical problem.

comment:5 Changed 5 years ago by pcapriotti

Milestone: 7.6.1
Resolution: duplicate
Status: newclosed

See #7034 and #5692.

Note: See TracTickets for help on using tickets.