Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf23a9b8

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 theresult has more than 16383 digits after the decimal point by roundingthe result. Overflow errors should only occur if the result has toomany digits *before* the decimal point.Discussion:https://postgr.es/m/CAEZATCUmeFWCrq2dNzZpRj5+6LfN85jYiDoqm+ucSXhb9U2TbA@mail.gmail.com
1 parent32d0bdb commitf23a9b8

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
@@ -205,6 +205,7 @@ struct NumericData
205205
*/
206206

207207
#defineNUMERIC_DSCALE_MASK0x3FFF
208+
#defineNUMERIC_DSCALE_MAXNUMERIC_DSCALE_MASK
208209

209210
#defineNUMERIC_SIGN(n) \
210211
(NUMERIC_IS_SHORT(n) ? \
@@ -2549,14 +2550,21 @@ numeric_mul_opt_error(Numeric num1, Numeric num2, bool *have_error)
25492550
* Unlike add_var() and sub_var(), mul_var() will round its result. In the
25502551
* case of numeric_mul(), which is invoked for the * operator on numerics,
25512552
* we request exact representation for the product (rscale = sum(dscale of
2552-
* arg1, dscale of arg2)).
2553+
* arg1, dscale of arg2)). If the exact result has more digits after the
2554+
* decimal point than can be stored in a numeric, we round it. Rounding
2555+
* after computing the exact result ensures that the final result is
2556+
* correctly rounded (rounding in mul_var() using a truncated product
2557+
* would not guarantee this).
25532558
*/
25542559
init_var_from_num(num1,&arg1);
25552560
init_var_from_num(num2,&arg2);
25562561

25572562
init_var(&result);
25582563
mul_var(&arg1,&arg2,&result,arg1.dscale+arg2.dscale);
25592564

2565+
if (result.dscale>NUMERIC_DSCALE_MAX)
2566+
round_var(&result,NUMERIC_DSCALE_MAX);
2567+
25602568
res=make_result_opt_error(&result,have_error);
25612569

25622570
free_var(&result);

‎src/test/regress/expected/numeric.out

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1498,6 +1498,12 @@ select 4769999999999999999999999999999999999999999999999999999999999999999999999
14981498
47699999999999999999999999999999999999999999999999999999999999999999999999999999999999985230000000000000000000000000000000000000000000000000000000000000000000000000000000000001
14991499
(1 row)
15001500

1501+
select trim_scale((0.1 - 2e-16383) * (0.1 - 3e-16383));
1502+
trim_scale
1503+
------------
1504+
0.01
1505+
(1 row)
1506+
15011507
--
15021508
-- Test some corner cases for division
15031509
--

‎src/test/regress/sql/numeric.sql

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

865865
select4769999999999999999999999999999999999999999999999999999999999999999999999999999999999999*9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
866866

867+
select trim_scale((0.1- 2e-16383)* (0.1- 3e-16383));
868+
867869
--
868870
-- Test some corner cases for division
869871
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp