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

Commit 3fd3347

Browse files
committed
Don't depend on -fwrapv semantics in pgbench's random() function.
Instead use the common/int.h functions to check for integer overflow in a more C-standard-compliant fashion. This is motivated by recent failures on buildfarm member moonjelly, where it appears that development-tip gcc is optimizing without regard to the -fwrapv switch. Presumably that's a gcc bug that will be fixed soon, but we might as well install cleaner coding here rather than wait. (This does not address the question of whether we'll ever be able to get rid of using -fwrapv. Testing shows that this spot is the only place where doing so creates visible regression test failures, but unfortunately that proves very little.) Back-patch to v12. The common/int.h functions exist in v11, but that branch doesn't use them in any client-side code. I judge that this case isn't interesting enough in the real world to take even a small risk of issues from being the first such use. Tom Lane and Fabien Coelho Discussion: https://postgr.es/m/73927.1624815543@sss.pgh.pa.us
1 parent b75c1f6 commit 3fd3347

File tree

1 file changed

+5
-3
lines changed

1 file changed

+5
-3
lines changed

src/bin/pgbench/pgbench.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2259,7 +2259,8 @@ evalStandardFunc(CState *st,
22592259
case PGBENCH_RANDOM_ZIPFIAN:
22602260
{
22612261
int64 imin,
2262-
imax;
2262+
imax,
2263+
delta;
22632264

22642265
Assert(nargs >= 2);
22652266

@@ -2268,12 +2269,13 @@ evalStandardFunc(CState *st,
22682269
return false;
22692270

22702271
/* check random range */
2271-
if (imin > imax)
2272+
if (unlikely(imin > imax))
22722273
{
22732274
fprintf(stderr, "empty range given to random\n");
22742275
return false;
22752276
}
2276-
else if (imax - imin < 0 || (imax - imin) + 1 < 0)
2277+
else if (unlikely(pg_sub_s64_overflow(imax, imin, &delta) ||
2278+
pg_add_s64_overflow(delta, 1, &delta)))
22772279
{
22782280
/* prevent int overflows in random functions */
22792281
fprintf(stderr, "random range is too large\n");

0 commit comments

Comments
 (0)