Skip to content

Commit e7fc488

Browse files
committed
Fix numeric_mul() overflow due to too many digits after decimal point.
This fixes an overflow error when using the numeric * operator if the result has more than 16383 digits after the decimal point by rounding the result. Overflow errors should only occur if the result has too many digits *before* the decimal point. Discussion: https://postgr.es/m/CAEZATCUmeFWCrq2dNzZpRj5+6LfN85jYiDoqm+ucSXhb9U2TbA@mail.gmail.com
1 parent 53c38a0 commit e7fc488

File tree

3 files changed

+17
-1
lines changed

3 files changed

+17
-1
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,7 @@ struct NumericData
233233
*/
234234

235235
#define NUMERIC_DSCALE_MASK 0x3FFF
236+
#define NUMERIC_DSCALE_MAX NUMERIC_DSCALE_MASK
236237

237238
#define NUMERIC_SIGN(n) \
238239
(NUMERIC_IS_SHORT(n) ? \
@@ -2958,14 +2959,21 @@ numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
29582959
* Unlike add_var() and sub_var(), mul_var() will round its result. In the
29592960
* case of numeric_mul(), which is invoked for the * operator on numerics,
29602961
* we request exact representation for the product (rscale = sum(dscale of
2961-
* arg1, dscale of arg2)).
2962+
* arg1, dscale of arg2)). If the exact result has more digits after the
2963+
* decimal point than can be stored in a numeric, we round it. Rounding
2964+
* after computing the exact result ensures that the final result is
2965+
* correctly rounded (rounding in mul_var() using a truncated product
2966+
* would not guarantee this).
29622967
*/
29632968
init_var_from_num(num1, &arg1);
29642969
init_var_from_num(num2, &arg2);
29652970

29662971
init_var(&result);
29672972
mul_var(&arg1, &arg2, &result, arg1.dscale + arg2.dscale);
29682973

2974+
if (result.dscale > NUMERIC_DSCALE_MAX)
2975+
round_var(&result, NUMERIC_DSCALE_MAX);
2976+
29692977
res = make_result_opt_error(&result, have_error);
29702978

29712979
free_var(&result);

src/test/regress/expected/numeric.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2145,6 +2145,12 @@ select 4769999999999999999999999999999999999999999999999999999999999999999999999
21452145
47699999999999999999999999999999999999999999999999999999999999999999999999999999999999985230000000000000000000000000000000000000000000000000000000000000000000000000000000000001
21462146
(1 row)
21472147

2148+
select trim_scale((0.1 - 2e-16383) * (0.1 - 3e-16383));
2149+
trim_scale
2150+
------------
2151+
0.01
2152+
(1 row)
2153+
21482154
--
21492155
-- Test some corner cases for division
21502156
--

src/test/regress/sql/numeric.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,6 +1044,8 @@ select 4770999999999999999999999999999999999999999999999999999999999999999999999
10441044

10451045
select 4769999999999999999999999999999999999999999999999999999999999999999999999999999999999999 * 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
10461046

1047+
select trim_scale((0.1 - 2e-16383) * (0.1 - 3e-16383));
1048+
10471049
--
10481050
-- Test some corner cases for division
10491051
--

0 commit comments

Comments
 (0)