#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 Difficulty: Unknown
Test Case: Blocked By:
Blocking: Related Tickets: #5688

Description

Computing

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 22 months ago by pcapriotti

  • Difficulty set to Unknown
  • Resolution set to fixed
  • Status changed from new to closed

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 22 months ago by andersk

  • Version changed from 7.4.2 to 7.4.1

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

comment:3 Changed 22 months ago by andersk

  • Resolution fixed deleted
  • Status changed from closed to new
  • Version changed from 7.4.1 to 7.4.2

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 22 months 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 22 months ago by pcapriotti

  • Milestone set to 7.6.1
  • Resolution set to duplicate
  • Status changed from new to closed

See #7034 and #5692.

Note: See TracTickets for help on using tickets.