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

Commit 258b770

Browse files
committed
Fix numeric_power() when the exponent is INT_MIN.
In power_var_int(), the computation of the number of significant digits to use in the computation used log(Abs(exp)), which isn't safe because Abs(exp) returns INT_MIN when exp is INT_MIN. Use fabs() instead of Abs(), so that the exponent is cast to a double before the absolute value is taken. Back-patch to 9.6, where this was introduced (by 7d9a473). Discussion: https://postgr.es/m/CAEZATCVd6pMkz=BrZEgBKyqqJrt2xghr=fNc8+Z=5xC6cgWrWA@mail.gmail.com
1 parent 7c98759 commit 258b770

File tree

3 files changed

+8
-1
lines changed

3 files changed

+8
-1
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8601,7 +8601,7 @@ power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
86018601
* to around log10(abs(exp)) digits, so work with this many extra digits
86028602
* of precision (plus a few more for good measure).
86038603
*/
8604-
sig_digits += (int) log(Abs(exp)) + 8;
8604+
sig_digits += (int) log(fabs(exp)) + 8;
86058605

86068606
/*
86078607
* Now we can proceed with the multiplications.

src/test/regress/expected/numeric.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1623,6 +1623,12 @@ select 0.12 ^ (-20);
16231623
2608405330458882702.5529619561355838
16241624
(1 row)
16251625

1626+
select 1.000000000123 ^ (-2147483648);
1627+
?column?
1628+
--------------------
1629+
0.7678656556403084
1630+
(1 row)
1631+
16261632
-- cases that used to error out
16271633
select 0.12 ^ (-25);
16281634
?column?

src/test/regress/sql/numeric.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -896,6 +896,7 @@ select 3.789 ^ 21;
896896
select 3.789 ^ 35;
897897
select 1.2 ^ 345;
898898
select 0.12 ^ (-20);
899+
select 1.000000000123 ^ (-2147483648);
899900

900901
-- cases that used to error out
901902
select 0.12 ^ (-25);

0 commit comments

Comments
 (0)