Skip to content

Commit e15c384

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 9e7d87c commit e15c384

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
@@ -9370,7 +9370,7 @@ power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale)
93709370
* to around log10(abs(exp)) digits, so work with this many extra digits
93719371
* of precision (plus a few more for good measure).
93729372
*/
9373-
sig_digits += (int) log(Abs(exp)) + 8;
9373+
sig_digits += (int) log(fabs(exp)) + 8;
93749374

93759375
/*
93769376
* 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
@@ -1674,6 +1674,12 @@ select 0.12 ^ (-20);
16741674
2608405330458882702.5529619561355838
16751675
(1 row)
16761676

1677+
select 1.000000000123 ^ (-2147483648);
1678+
?column?
1679+
--------------------
1680+
0.7678656556403084
1681+
(1 row)
1682+
16771683
-- cases that used to error out
16781684
select 0.12 ^ (-25);
16791685
?column?

src/test/regress/sql/numeric.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ select 3.789 ^ 21;
909909
select 3.789 ^ 35;
910910
select 1.2 ^ 345;
911911
select 0.12 ^ (-20);
912+
select 1.000000000123 ^ (-2147483648);
912913

913914
-- cases that used to error out
914915
select 0.12 ^ (-25);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy