Skip to content

Commit bc43b7c

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 5a3574d commit bc43b7c

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

1029510295
/*
1029610296
* 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
@@ -2321,6 +2321,12 @@ select 0.12 ^ (-20);
23212321
2608405330458882702.5529619561355838
23222322
(1 row)
23232323

2324+
select 1.000000000123 ^ (-2147483648);
2325+
?column?
2326+
--------------------
2327+
0.7678656556403084
2328+
(1 row)
2329+
23242330
-- cases that used to error out
23252331
select 0.12 ^ (-25);
23262332
?column?

src/test/regress/sql/numeric.sql

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1089,6 +1089,7 @@ select 3.789 ^ 21;
10891089
select 3.789 ^ 35;
10901090
select 1.2 ^ 345;
10911091
select 0.12 ^ (-20);
1092+
select 1.000000000123 ^ (-2147483648);
10921093

10931094
-- cases that used to error out
10941095
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