DoubleStream.count() overflow bug

Paul Sandoz paul.sandoz at oracle.com
Mon Jan 6 01:32:49 PST 2014


Hi Jan,

Thanks, this is definitely a bug and an embarrassing one too:

    public final long count() {
        return mapToObj(e -> null).mapToInt(e -> 1).sum();
    }

Don't quite know what one was smoking when one wrote that. It should be:

    public final long count() {
        return mapToLong(e -> 1L).sum();
    }

Fix on the way...

  https://bugs.openjdk.java.net/browse/JDK-8031187

Paul.

On Dec 24, 2013, at 11:56 PM, Jan Beernink <jan.beernink at gmail.com> wrote:

> Hi,
> 
> I believe I may have discovered a small bug in the DoubleStream.count() operation. On JDK 8 b120, the following code will return -2147483648:
> 
> DoubleStream.generate(() -> 0.0)
>                       .limit(2147483648L)
>                       .count()
> 
> I had a look at the code of java.util.stream.DoublePipeline.count() and noticed that, although the return type is long, each element in the stream is (indirectly) mapped to an int of value 1 and then these ints are summed using IntStream.sum(). IntPipeline, LongPipeline and ReferencePipeline all count the number of elements in a similar way, but use longs instead of ints and correctly return 2147483648.
> 
> Regards,
> 
> Jan Beernink
> 



More information about the lambda-dev mailing list