Subject: | Primes() i*i loop vs bits=2^32-1 |

Date: | Sat, 06 Nov 2010 07:38:02 +1100 |

To: | bug-Bit-Vector@rt.cpan.org |

From: | Kevin Ryde <user42@zip.com.au> |

In Bit::Vector 7.1 BitVector_Primes(), if bits=2^32-1 then I think the
i*i in the loop test
(j = i * i) < bits
may overflow 32 bits. The loop runs i=65535 as it should, but the next
i=65537 has i*i wrapping around to 131073, so i*i < bits is satisfied
and it doesn't stop as intended.
I suspect it may even end up an infinite loop if i*i never comes out
equal to "bits".
In any case maybe it could have a pre-calculated loop limit like
floor(sqrt(bits)), or maybe an extra i <= floor(sqrt(UINT_MAX)) worked
in to ensure i*i<=UINT_MAX.