Fix corner-case 64-bit integer subtraction bug on some platforms.
authorDean Rasheed <[email protected]>
Thu, 9 Nov 2023 09:50:23 +0000 (09:50 +0000)
committerDean Rasheed <[email protected]>
Thu, 9 Nov 2023 09:50:23 +0000 (09:50 +0000)
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

src/include/common/int.h
src/test/regress/expected/int8.out
src/test/regress/sql/int8.sql

index 450800894ed6598222d11bbf8fcc6e441758cfbb..487124473d25b206a985a9742f39b348f50a5324 100644 (file)
@@ -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;
index 9542d622bab88e999b577a5aa347f991172fa49e..fddc09f6305a8a82d345b5bc1f5d68d99c5912ab 100644 (file)
@@ -679,6 +679,8 @@ select -('-9223372036854775807'::int8);
 
 select -('-9223372036854775808'::int8);
 ERROR:  bigint out of range
+select 0::int8 - '-9223372036854775808'::int8;
+ERROR:  bigint out of range
 select '9223372036854775800'::int8 + '9223372036854775800'::int8;
 ERROR:  bigint out of range
 select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;
index 33f664dd02c01971fabc4db8e41ff49e41b05325..fffb28906a1e616401d22c8a63b642effd145fca 100644 (file)
@@ -132,6 +132,7 @@ select '9223372036854775808'::int8;
 
 select -('-9223372036854775807'::int8);
 select -('-9223372036854775808'::int8);
+select 0::int8 - '-9223372036854775808'::int8;
 
 select '9223372036854775800'::int8 + '9223372036854775800'::int8;
 select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;