[PATCH 1/2] Fix INT_MIN % -1 overflow in int8mod().

From: Xi Wang <xi(dot)wang(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: [PATCH 1/2] Fix INT_MIN % -1 overflow in int8mod().
Date: 2012-11-14 22:05:28
Message-ID: 50A415A8.2060105@gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Return 0 for x % -1 instead of throwing an exception (e.g., when x
is INT_MIN).

Suggested by Tom Lane.
---
src/backend/utils/adt/int8.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/src/backend/utils/adt/int8.c b/src/backend/utils/adt/int8.c
index 0e59956..a30ab36 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 (arg2 == -1)
+ PG_RETURN_INT64(0);
+
/* No overflow is possible */

PG_RETURN_INT64(arg1 % arg2);
--
1.7.10.4

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Xi Wang 2012-11-14 22:07:28 [PATCH 2/2] Clean up INT_MIN % -1 overflow in int4mod().
Previous Message Tom Lane 2012-11-14 22:03:07 Re: [PATCH] Fix INT_MIN % -1 overflow in int8mod().