Skip to content

Commit be567de

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 741deb2 commit be567de

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
@@ -2278,7 +2278,8 @@ evalStandardFunc(CState *st,
22782278
case PGBENCH_RANDOM_ZIPFIAN:
22792279
{
22802280
int64 imin,
2281-
imax;
2281+
imax,
2282+
delta;
22822283

22832284
Assert(nargs >= 2);
22842285

@@ -2287,12 +2288,13 @@ evalStandardFunc(CState *st,
22872288
return false;
22882289

22892290
/* check random range */
2290-
if (imin > imax)
2291+
if (unlikely(imin > imax))
22912292
{
22922293
pg_log_error("empty range given to random");
22932294
return false;
22942295
}
2295-
else if (imax - imin < 0 || (imax - imin) + 1 < 0)
2296+
else if (unlikely(pg_sub_s64_overflow(imax, imin, &delta) ||
2297+
pg_add_s64_overflow(delta, 1, &delta)))
22962298
{
22972299
/* prevent int overflows in random functions */
22982300
pg_log_error("random range is too large");

0 commit comments

Comments
 (0)