Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8acfc75

Browse files
committed
Tweak the overflow checks in integer division functions to complain if the
machine produces zero (rather than the more usual minimum-possible-integer) for the only possible overflow case. This has been seen to occur for at least some word widths on some hardware, and it's cheap enough to check for everywhere. Per Peter's analysis of buildfarm reports. This could be back-patched, but in the absence of any gripes from the field I doubt it's worth the trouble.
1 parent 1e4b038 commit 8acfc75

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

src/backend/utils/adt/int.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.82 2008/06/17 19:10:56 tgl Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/int.c,v 1.83 2008/10/05 23:18:37 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -735,9 +735,10 @@ int4div(PG_FUNCTION_ARGS)
735735
/*
736736
* Overflow check. The only possible overflow case is for arg1 = INT_MIN,
737737
* arg2 = -1, where the correct result is -INT_MIN, which can't be
738-
* represented on a two's-complement machine.
738+
* represented on a two's-complement machine. Most machines produce
739+
* INT_MIN but it seems some produce zero.
739740
*/
740-
if (arg2 == -1 && arg1 < 0 && result < 0)
741+
if (arg2 == -1 && arg1 < 0 && result <= 0)
741742
ereport(ERROR,
742743
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
743744
errmsg("integer out of range")));
@@ -863,9 +864,10 @@ int2div(PG_FUNCTION_ARGS)
863864
/*
864865
* Overflow check. The only possible overflow case is for arg1 =
865866
* SHRT_MIN, arg2 = -1, where the correct result is -SHRT_MIN, which can't
866-
* be represented on a two's-complement machine.
867+
* be represented on a two's-complement machine. Most machines produce
868+
* SHRT_MIN but it seems some produce zero.
867869
*/
868-
if (arg2 == -1 && arg1 < 0 && result < 0)
870+
if (arg2 == -1 && arg1 < 0 && result <= 0)
869871
ereport(ERROR,
870872
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
871873
errmsg("smallint out of range")));
@@ -1041,9 +1043,10 @@ int42div(PG_FUNCTION_ARGS)
10411043
/*
10421044
* Overflow check. The only possible overflow case is for arg1 = INT_MIN,
10431045
* arg2 = -1, where the correct result is -INT_MIN, which can't be
1044-
* represented on a two's-complement machine.
1046+
* represented on a two's-complement machine. Most machines produce
1047+
* INT_MIN but it seems some produce zero.
10451048
*/
1046-
if (arg2 == -1 && arg1 < 0 && result < 0)
1049+
if (arg2 == -1 && arg1 < 0 && result <= 0)
10471050
ereport(ERROR,
10481051
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10491052
errmsg("integer out of range")));

src/backend/utils/adt/int8.c

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.70 2008/06/17 19:10:56 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/utils/adt/int8.c,v 1.71 2008/10/05 23:18:37 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -608,9 +608,10 @@ int8div(PG_FUNCTION_ARGS)
608608
/*
609609
* Overflow check. The only possible overflow case is for arg1 =
610610
* INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
611-
* can't be represented on a two's-complement machine.
611+
* can't be represented on a two's-complement machine. Most machines
612+
* produce INT64_MIN but it seems some produce zero.
612613
*/
613-
if (arg2 == -1 && arg1 < 0 && result < 0)
614+
if (arg2 == -1 && arg1 < 0 && result <= 0)
614615
ereport(ERROR,
615616
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
616617
errmsg("bigint out of range")));
@@ -830,9 +831,10 @@ int84div(PG_FUNCTION_ARGS)
830831
/*
831832
* Overflow check. The only possible overflow case is for arg1 =
832833
* INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
833-
* can't be represented on a two's-complement machine.
834+
* can't be represented on a two's-complement machine. Most machines
835+
* produce INT64_MIN but it seems some produce zero.
834836
*/
835-
if (arg2 == -1 && arg1 < 0 && result < 0)
837+
if (arg2 == -1 && arg1 < 0 && result <= 0)
836838
ereport(ERROR,
837839
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
838840
errmsg("bigint out of range")));
@@ -1008,9 +1010,10 @@ int82div(PG_FUNCTION_ARGS)
10081010
/*
10091011
* Overflow check. The only possible overflow case is for arg1 =
10101012
* INT64_MIN, arg2 = -1, where the correct result is -INT64_MIN, which
1011-
* can't be represented on a two's-complement machine.
1013+
* can't be represented on a two's-complement machine. Most machines
1014+
* produce INT64_MIN but it seems some produce zero.
10121015
*/
1013-
if (arg2 == -1 && arg1 < 0 && result < 0)
1016+
if (arg2 == -1 && arg1 < 0 && result <= 0)
10141017
ereport(ERROR,
10151018
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10161019
errmsg("bigint out of range")));

0 commit comments

Comments
 (0)