aboutsummaryrefslogtreecommitdiff
path: root/src/include/common/int.h
diff options
context:
space:
mode:
authorDean Rasheed <dean.a.rasheed@gmail.com>2023-11-09 09:53:05 +0000
committerDean Rasheed <dean.a.rasheed@gmail.com>2023-11-09 09:53:05 +0000
commitc396aca2b712f7872dcc4641a2a0fa553f232f76 (patch)
tree55d944e4fa7bfa450ec1c0b5355d332ee560fa02 /src/include/common/int.h
parent179c4639cf1dcbe54f3469d7f44a11f172332893 (diff)
downloadpostgresql-c396aca2b712f7872dcc4641a2a0fa553f232f76.tar.gz
postgresql-c396aca2b712f7872dcc4641a2a0fa553f232f76.zip
Fix corner-case 64-bit integer subtraction bug on some platforms.
When computing "0 - INT64_MIN", most platforms would report an overflow error, which is correct. However, platforms without integer overflow builtins or 128-bit integers would fail to spot the overflow, and incorrectly return INT64_MIN. Back-patch to all supported branches. Patch be me. Thanks to Jian He for initial investigation, and Laurenz Albe and Tom Lane for review. Discussion: https://postgr.es/m/CAEZATCUNK-AZSD0jVdgkk0N%3DNcAXBWeAEX-QU9AnJPensikmdQ%40mail.gmail.com
Diffstat (limited to 'src/include/common/int.h')
-rw-r--r--src/include/common/int.h6
1 files changed, 5 insertions, 1 deletions
diff --git a/src/include/common/int.h b/src/include/common/int.h
index 450800894ed..487124473d2 100644
--- a/src/include/common/int.h
+++ b/src/include/common/int.h
@@ -200,8 +200,12 @@ pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
*result = (int64) res;
return false;
#else
+ /*
+ * Note: overflow is also possible when a == 0 and b < 0 (specifically,
+ * when b == PG_INT64_MIN).
+ */
if ((a < 0 && b > 0 && a < PG_INT64_MIN + b) ||
- (a > 0 && b < 0 && a > PG_INT64_MAX + b))
+ (a >= 0 && b < 0 && a > PG_INT64_MAX + b))
{
*result = 0x5EED; /* to avoid spurious warnings */
return true;