Skip to content

Commit 01697e9

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 ba135fa commit 01697e9

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
@@ -2450,7 +2450,8 @@ evalStandardFunc(CState *st,
24502450
case PGBENCH_RANDOM_ZIPFIAN:
24512451
{
24522452
int64 imin,
2453-
imax;
2453+
imax,
2454+
delta;
24542455

24552456
Assert(nargs >= 2);
24562457

@@ -2459,12 +2460,13 @@ evalStandardFunc(CState *st,
24592460
return false;
24602461

24612462
/* check random range */
2462-
if (imin > imax)
2463+
if (unlikely(imin > imax))
24632464
{
24642465
pg_log_error("empty range given to random");
24652466
return false;
24662467
}
2467-
else if (imax - imin < 0 || (imax - imin) + 1 < 0)
2468+
else if (unlikely(pg_sub_s64_overflow(imax, imin, &delta) ||
2469+
pg_add_s64_overflow(delta, 1, &delta)))
24682470
{
24692471
/* prevent int overflows in random functions */
24702472
pg_log_error("random range is too large");

0 commit comments

Comments
 (0)