Commit d7c19d68 authored by Tom Lane's avatar Tom Lane

Make sampler_random_fract() actually obey its API contract.

This function is documented to return a value in the range (0,1),
which is what its predecessor anl_random_fract() did.  However, the
new version depends on pg_erand48() which returns a value in [0,1).
The possibility of returning zero creates hazards of division by zero
or trying to compute log(0) at some call sites, and it might well
break third-party modules using anl_random_fract() too.  So let's
change it to never return zero.  Spotted by Coverity.

Michael Paquier, cosmetically adjusted by me
parent 82173708
...@@ -237,7 +237,14 @@ sampler_random_init_state(long seed, SamplerRandomState randstate) ...@@ -237,7 +237,14 @@ sampler_random_init_state(long seed, SamplerRandomState randstate)
double double
sampler_random_fract(SamplerRandomState randstate) sampler_random_fract(SamplerRandomState randstate)
{ {
return pg_erand48(randstate); double res;
/* pg_erand48 returns a value in [0.0 - 1.0), so we must reject 0 */
do
{
res = pg_erand48(randstate);
} while (res == 0.0);
return res;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment