[PATCH] FIx alloc_var() ndigits thinko
Started by Joel Jacobsonalmost 3 years ago1 messages
Hi,
I came across another harmless thinko in numeric.c.
It is harmless since 20/DEC_DIGITS and 40/DEC_DIGITS happens to be exactly 5 and 10 since DEC_DIGITS == 4,
but should be fixed anyway for correctness IMO.
- alloc_var(var, 20 / DEC_DIGITS);
+ alloc_var(var, (20 + DEC_DIGITS - 1) / DEC_DIGITS);
- alloc_var(var, 40 / DEC_DIGITS);
+ alloc_var(var, (40 + DEC_DIGITS - 1) / DEC_DIGITS);
/Joel
Attachments:
0001-fix-alloc-var-ndigits-thinko.patchapplication/octet-stream; name="=?UTF-8?Q?0001-fix-alloc-var-ndigits-thinko.patch?="Download
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index a83feea396..8d575f1853 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -8028,7 +8028,7 @@ int64_to_numericvar(int64 val, NumericVar *var)
int ndigits;
/* int64 can require at most 19 decimal digits; add one for safety */
- alloc_var(var, 20 / DEC_DIGITS);
+ alloc_var(var, (20 + DEC_DIGITS - 1) / DEC_DIGITS);
if (val < 0)
{
var->sign = NUMERIC_NEG;
@@ -8219,7 +8219,7 @@ int128_to_numericvar(int128 val, NumericVar *var)
int ndigits;
/* int128 can require at most 39 decimal digits; add one for safety */
- alloc_var(var, 40 / DEC_DIGITS);
+ alloc_var(var, (40 + DEC_DIGITS - 1) / DEC_DIGITS);
if (val < 0)
{
var->sign = NUMERIC_NEG;