[PATCH 2/3] Fix x + 1 < x overflow checks - Mailing list pgsql-hackers

From Xi Wang
Subject [PATCH 2/3] Fix x + 1 < x overflow checks
Date
Msg-id 5101014F.7080202@gmail.com
Whole thread Raw
In response to [PATCH 0/3] Work around icc miscompilation  (Xi Wang <xi.wang@gmail.com>)
List pgsql-hackers
icc optimizes away x + 1 < x because signed integer overflow is
undefined behavior in C.  Instead, simply check if x is INT_MAX.
---src/backend/utils/adt/float.c |    8 ++++----1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/backend/utils/adt/float.c b/src/backend/utils/adt/float.c
index b73e0d5..344b092 100644
--- a/src/backend/utils/adt/float.c
+++ b/src/backend/utils/adt/float.c
@@ -2764,12 +2764,12 @@ width_bucket_float8(PG_FUNCTION_ARGS)            result = 0;        else if (operand >= bound2)
      {
 
-            result = count + 1;            /* check for overflow */
-            if (result < count)
+            if (count == INT_MAX)                ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),                        errmsg("integer out of range")));
 
+            result = count + 1;        }        else            result = ((float8) count * (operand - bound1) /
(bound2- bound1)) + 1;
 
@@ -2780,12 +2780,12 @@ width_bucket_float8(PG_FUNCTION_ARGS)            result = 0;        else if (operand <= bound2)
      {
 
-            result = count + 1;            /* check for overflow */
-            if (result < count)
+            if (count == INT_MAX)                ereport(ERROR,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),                        errmsg("integer out of range")));
 
+            result = count + 1;        }        else            result = ((float8) count * (bound1 - operand) /
(bound1- bound2)) + 1;
 
-- 
1.7.10.4




pgsql-hackers by date:

Previous
From: Xi Wang
Date:
Subject: [PATCH 1/3] Fix x + y < x overflow checks
Next
From: Xi Wang
Date:
Subject: [PATCH 3/3] Fix overflow checking in repeat()