Fix numeric_mul() overflow due to too many digits after decimal point.
authorDean Rasheed <[email protected]>
Sat, 10 Jul 2021 11:51:22 +0000 (12:51 +0100)
committerDean Rasheed <[email protected]>
Sat, 10 Jul 2021 11:51:22 +0000 (12:51 +0100)
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

src/backend/utils/adt/numeric.c
src/test/regress/expected/numeric.out
src/test/regress/sql/numeric.sql

index da639be40bfebb47567b6e3c2174699a65c5f319..8ffeb6166e3fefa65ae402248669b3370ca25df7 100644 (file)
@@ -202,6 +202,7 @@ struct NumericData
  */
 
 #define NUMERIC_DSCALE_MASK            0x3FFF
+#define NUMERIC_DSCALE_MAX         NUMERIC_DSCALE_MASK
 
 #define NUMERIC_SIGN(n) \
    (NUMERIC_IS_SHORT(n) ? \
@@ -2291,7 +2292,11 @@ numeric_mul(PG_FUNCTION_ARGS)
     * Unlike add_var() and sub_var(), mul_var() will round its result. In the
     * case of numeric_mul(), which is invoked for the * operator on numerics,
     * we request exact representation for the product (rscale = sum(dscale of
-    * arg1, dscale of arg2)).
+    * arg1, dscale of arg2)).  If the exact result has more digits after the
+    * decimal point than can be stored in a numeric, we round it.  Rounding
+    * after computing the exact result ensures that the final result is
+    * correctly rounded (rounding in mul_var() using a truncated product
+    * would not guarantee this).
     */
    init_var_from_num(num1, &arg1);
    init_var_from_num(num2, &arg2);
@@ -2299,6 +2304,9 @@ numeric_mul(PG_FUNCTION_ARGS)
    init_var(&result);
    mul_var(&arg1, &arg2, &result, arg1.dscale + arg2.dscale);
 
+   if (result.dscale > NUMERIC_DSCALE_MAX)
+       round_var(&result, NUMERIC_DSCALE_MAX);
+
    res = make_result(&result);
 
    free_var(&result);
index ae1daa5744e99cf27e48c552b25232deb04df21f..bbc7f1c0a08c17a9cd6744ba81368cfd6dd12342 100644 (file)
@@ -1381,6 +1381,12 @@ select 4769999999999999999999999999999999999999999999999999999999999999999999999
  47699999999999999999999999999999999999999999999999999999999999999999999999999999999999985230000000000000000000000000000000000000000000000000000000000000000000000000000000000001
 (1 row)
 
+select (0.1 - 2e-16383) * (0.1 - 3e-16383) = 0.01 as rounds_to_point_zero_one;
+ rounds_to_point_zero_one 
+--------------------------
+ t
+(1 row)
+
 --
 -- Test some corner cases for division
 --
index d6ff9b8a399d67c9bd8e0ce939992405cfd3835d..d0d05957e615d4c9fc6ed68e0afd85bd639bf596 100644 (file)
@@ -841,6 +841,8 @@ select 4770999999999999999999999999999999999999999999999999999999999999999999999
 
 select 4769999999999999999999999999999999999999999999999999999999999999999999999999999999999999 * 9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999;
 
+select (0.1 - 2e-16383) * (0.1 - 3e-16383) = 0.01 as rounds_to_point_zero_one;
+
 --
 -- Test some corner cases for division
 --