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

Lines changed: 8 additions & 1 deletion
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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy