Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d7c19d6

Browse files
committed
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
1 parent 8217370 commit d7c19d6

File tree

1 file changed

+8
-1
lines changed

1 file changed

+8
-1
lines changed

src/backend/utils/misc/sampling.c

+8-1
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,14 @@ sampler_random_init_state(long seed, SamplerRandomState randstate)
237237
double
238238
sampler_random_fract(SamplerRandomState randstate)
239239
{
240-
return pg_erand48(randstate);
240+
double res;
241+
242+
/* pg_erand48 returns a value in [0.0 - 1.0), so we must reject 0 */
243+
do
244+
{
245+
res = pg_erand48(randstate);
246+
} while (res == 0.0);
247+
return res;
241248
}
242249

243250

0 commit comments

Comments
 (0)