Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit308a69a

Browse files
committed
Fix corner-case 64-bit integer subtraction bug on some platforms.
When computing "0 - INT64_MIN", most platforms would report anoverflow error, which is correct. However, platforms without integeroverflow 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 LaurenzAlbe and Tom Lane for review.Discussion:https://postgr.es/m/CAEZATCUNK-AZSD0jVdgkk0N%3DNcAXBWeAEX-QU9AnJPensikmdQ%40mail.gmail.com
1 parent0e28091 commit308a69a

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

‎src/include/common/int.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,12 @@ pg_sub_s64_overflow(int64 a, int64 b, int64 *result)
200200
*result= (int64)res;
201201
return false;
202202
#else
203+
/*
204+
* Note: overflow is also possible when a == 0 and b < 0 (specifically,
205+
* when b == PG_INT64_MIN).
206+
*/
203207
if ((a<0&&b>0&&a<PG_INT64_MIN+b)||
204-
(a>0&&b<0&&a>PG_INT64_MAX+b))
208+
(a >=0&&b<0&&a>PG_INT64_MAX+b))
205209
{
206210
*result=0x5EED;/* to avoid spurious warnings */
207211
return true;

‎src/test/regress/expected/int8.out

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -654,6 +654,8 @@ select -('-9223372036854775807'::int8);
654654

655655
select -('-9223372036854775808'::int8);
656656
ERROR: bigint out of range
657+
select 0::int8 - '-9223372036854775808'::int8;
658+
ERROR: bigint out of range
657659
select '9223372036854775800'::int8 + '9223372036854775800'::int8;
658660
ERROR: bigint out of range
659661
select '-9223372036854775800'::int8 + '-9223372036854775800'::int8;

‎src/test/regress/sql/int8.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ select '9223372036854775808'::int8;
126126

127127
select-('-9223372036854775807'::int8);
128128
select-('-9223372036854775808'::int8);
129+
select0::int8-'-9223372036854775808'::int8;
129130

130131
select'9223372036854775800'::int8+'9223372036854775800'::int8;
131132
select'-9223372036854775800'::int8+'-9223372036854775800'::int8;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp