Skip to content

Commit c33d1a8

Browse files
committed
pgbench: Install guard against overflow when dividing by -1.
Commit 64f5edc fixed the same hazard on master; this is a backport, but the modulo operator does not exist in older releases. Michael Paquier
1 parent aa223a0 commit c33d1a8

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

contrib/pgbench/pgbench.c

+29-1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@
5252
#ifndef INT64_MAX
5353
#define INT64_MAX INT64CONST(0x7FFFFFFFFFFFFFFF)
5454
#endif
55+
#ifndef INT64_MIN
56+
#define INT64_MIN (-INT64CONST(0x7FFFFFFFFFFFFFFF) - 1)
57+
#endif
58+
5559

5660
/*
5761
* Multi-platform pthread implementations
@@ -1510,13 +1514,37 @@ doCustom(TState *thread, CState *st, instr_time *conn_time, FILE *logfile, AggVa
15101514
snprintf(res, sizeof(res), INT64_FORMAT, ope1 * ope2);
15111515
else if (strcmp(argv[3], "/") == 0)
15121516
{
1517+
int64 operes;
1518+
15131519
if (ope2 == 0)
15141520
{
15151521
fprintf(stderr, "%s: division by zero\n", argv[0]);
15161522
st->ecnt++;
15171523
return true;
15181524
}
1519-
snprintf(res, sizeof(res), INT64_FORMAT, ope1 / ope2);
1525+
/*
1526+
* INT64_MIN / -1 is problematic, since the result can't
1527+
* be represented on a two's-complement machine. Some
1528+
* machines produce INT64_MIN, some produce zero, some
1529+
* throw an exception. We can dodge the problem by
1530+
* recognizing that division by -1 is the same as
1531+
* negation.
1532+
*/
1533+
if (ope2 == -1)
1534+
{
1535+
operes = -ope1;
1536+
1537+
/* overflow check (needed for INT64_MIN) */
1538+
if (ope1 == INT64_MIN)
1539+
{
1540+
fprintf(stderr, "bigint out of range\n");
1541+
st->ecnt++;
1542+
return true;
1543+
}
1544+
}
1545+
else
1546+
operes = ope1 / ope2;
1547+
snprintf(res, sizeof(res), INT64_FORMAT, operes);
15201548
}
15211549
else
15221550
{

0 commit comments

Comments
 (0)