aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRobert Haas <rhaas@postgresql.org>2016-02-01 08:23:41 -0500
committerRobert Haas <rhaas@postgresql.org>2016-02-01 08:26:07 -0500
commit829757c8a225e5b81a398823d77fa6c0809cf863 (patch)
tree366db7112147f489d0c7ff1081e8aba1e8ba45dc
parent40482e606733675eb9e5b2f7221186cf81352da1 (diff)
downloadpostgresql-829757c8a225e5b81a398823d77fa6c0809cf863.tar.gz
postgresql-829757c8a225e5b81a398823d77fa6c0809cf863.zip
pgbench: Install guards against obscure overflow conditions.
Dividing INT_MIN by -1 or taking INT_MIN modulo -1 can sometimes cause floating-point exceptions or otherwise misbehave. Fabien Coelho and Michael Paquier
-rw-r--r--src/bin/pgbench/pgbench.c36
1 files changed, 34 insertions, 2 deletions
diff --git a/src/bin/pgbench/pgbench.c b/src/bin/pgbench/pgbench.c
index 1a3ba032822..1e1806fad9a 100644
--- a/src/bin/pgbench/pgbench.c
+++ b/src/bin/pgbench/pgbench.c
@@ -961,7 +961,29 @@ evaluateExpr(CState *st, PgBenchExpr *expr, int64 *retval)
fprintf(stderr, "division by zero\n");
return false;
}
- *retval = lval / rval;
+
+ /*
+ * INT64_MIN / -1 is problematic, since the result
+ * can't be represented on a two's-complement machine.
+ * Some machines produce INT64_MIN, some produce zero,
+ * some throw an exception. We can dodge the problem
+ * by recognizing that division by -1 is the same as
+ * negation.
+ */
+ if (rval == -1)
+ {
+ *retval = -lval;
+
+ /* overflow check (needed for INT64_MIN) */
+ if (lval == PG_INT64_MIN)
+ {
+ fprintf(stderr, "bigint out of range\n");
+ return false;
+ }
+ }
+ else
+ *retval = lval / rval;
+
return true;
case '%':
@@ -970,7 +992,17 @@ evaluateExpr(CState *st, PgBenchExpr *expr, int64 *retval)
fprintf(stderr, "division by zero\n");
return false;
}
- *retval = lval % rval;
+
+ /*
+ * Some machines throw a floating-point exception for
+ * INT64_MIN % -1. Dodge that problem by noting that
+ * any value modulo -1 is 0.
+ */
+ if (rval == -1)
+ *retval = 0;
+ else
+ *retval = lval % rval;
+
return true;
}