From e15c384d7acaa2d7d967f2d8feb6bb0d3b793b3d Mon Sep 17 00:00:00 2001 From: Dean Rasheed Date: Tue, 5 Jan 2021 11:08:59 +0000 Subject: [PATCH] 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 7d9a4737c2). Discussion: https://postgr.es/m/CAEZATCVd6pMkz=BrZEgBKyqqJrt2xghr=fNc8+Z=5xC6cgWrWA@mail.gmail.com --- src/backend/utils/adt/numeric.c | 2 +- src/test/regress/expected/numeric.out | 6 ++++++ src/test/regress/sql/numeric.sql | 1 + 3 files changed, 8 insertions(+), 1 deletion(-) diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c index 553e261ed0..7582aa3043 100644 --- a/src/backend/utils/adt/numeric.c +++ b/src/backend/utils/adt/numeric.c @@ -9370,7 +9370,7 @@ power_var_int(const NumericVar *base, int exp, NumericVar *result, int rscale) * to around log10(abs(exp)) digits, so work with this many extra digits * of precision (plus a few more for good measure). */ - sig_digits += (int) log(Abs(exp)) + 8; + sig_digits += (int) log(fabs(exp)) + 8; /* * Now we can proceed with the multiplications. diff --git a/src/test/regress/expected/numeric.out b/src/test/regress/expected/numeric.out index c7fe63d037..c05bf7b8e6 100644 --- a/src/test/regress/expected/numeric.out +++ b/src/test/regress/expected/numeric.out @@ -1674,6 +1674,12 @@ select 0.12 ^ (-20); 2608405330458882702.5529619561355838 (1 row) +select 1.000000000123 ^ (-2147483648); + ?column? +-------------------- + 0.7678656556403084 +(1 row) + -- cases that used to error out select 0.12 ^ (-25); ?column? diff --git a/src/test/regress/sql/numeric.sql b/src/test/regress/sql/numeric.sql index 41475a9a24..6fa8d3fcc0 100644 --- a/src/test/regress/sql/numeric.sql +++ b/src/test/regress/sql/numeric.sql @@ -909,6 +909,7 @@ select 3.789 ^ 21; select 3.789 ^ 35; select 1.2 ^ 345; select 0.12 ^ (-20); +select 1.000000000123 ^ (-2147483648); -- cases that used to error out select 0.12 ^ (-25);