[PATCH] Fix INT_MIN % -1 overflow in int8mod(). - Mailing list pgsql-hackers

From Xi Wang
Subject [PATCH] Fix INT_MIN % -1 overflow in int8mod().
Date
Msg-id 1352925852-14714-1-git-send-email-xi.wang@gmail.com
Whole thread Raw
Responses Re: [PATCH] Fix INT_MIN % -1 overflow in int8mod().  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Return 0 for INT_MIN % -1 (64-bit) instead of throwing an exception.
This patch complements commit f9ac414c that fixed int4mod().
---src/backend/utils/adt/int8.c |    4 ++++src/include/c.h              |    7 +++++++2 files changed, 11
insertions(+)

diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c
index 0e59956..9da651b 100644
--- a/src/backend/utils/adt/int8.c
+++ b/src/backend/utils/adt/int8.c
@@ -649,6 +649,10 @@ int8mod(PG_FUNCTION_ARGS)        PG_RETURN_NULL();    }
+    /* SELECT ((-9223372036854775808)::int8) % (-1); causes a floating point exception */
+    if (arg1 == INT64_MIN && arg2 == -1)
+        PG_RETURN_INT64(0);
+    /* No overflow is possible */    PG_RETURN_INT64(arg1 % arg2);
diff --git a/src/include/c.h b/src/include/c.h
index a6c0e6e..d20ba8c 100644
--- a/src/include/c.h
+++ b/src/include/c.h
@@ -294,6 +294,13 @@ typedef unsigned long long int uint64;#define UINT64CONST(x) ((uint64) x)#endif
+#ifndef INT64_MAX
+#define INT64_MAX INT64CONST(9223372036854775807)
+#endif
+
+#ifndef INT64_MIN
+#define INT64_MIN (-INT64_MAX-1)
+#endif/* Select timestamp representation (float8 or int64) */#ifdef USE_INTEGER_DATETIMES
-- 
1.7.10.4




pgsql-hackers by date:

Previous
From: "Karl O. Pinc"
Date:
Subject: Re: Doc patch, further describe and-mask nature of the permission system
Next
From: Peter Geoghegan
Date:
Subject: Doc patch making firm recommendation for setting the value of commit_delay