summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/backend/utils/adt/numeric.c21
-rw-r--r--src/include/utils/numeric.h5
-rw-r--r--src/interfaces/ecpg/pgtypeslib/numeric.c3
3 files changed, 16 insertions, 13 deletions
diff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.c
index b99b6f58010..143bc1f9c7f 100644
--- a/src/backend/utils/adt/numeric.c
+++ b/src/backend/utils/adt/numeric.c
@@ -650,10 +650,6 @@ numeric_recv(PG_FUNCTION_ARGS)
init_var(&value);
len = (uint16) pq_getmsgint(buf, sizeof(uint16));
- if (len < 0 || len > NUMERIC_MAX_PRECISION + NUMERIC_MAX_RESULT_SCALE)
- ereport(ERROR,
- (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
- errmsg("invalid length in external \"numeric\" value")));
alloc_var(&value, len);
@@ -3316,12 +3312,19 @@ set_var_from_str(const char *str, const char *cp, NumericVar *dest)
errmsg("invalid input syntax for type numeric: \"%s\"",
str)));
cp = endptr;
- if (exponent > NUMERIC_MAX_PRECISION ||
- exponent < -NUMERIC_MAX_PRECISION)
+
+ /*
+ * At this point, dweight and dscale can't be more than about
+ * INT_MAX/2 due to the MaxAllocSize limit on string length, so
+ * constraining the exponent similarly should be enough to prevent
+ * integer overflow in this function. If the value is too large to
+ * fit in storage format, make_result() will complain about it later;
+ * for consistency use the same ereport errcode/text as make_result().
+ */
+ if (exponent >= INT_MAX / 2 || exponent <= -(INT_MAX / 2))
ereport(ERROR,
- (errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
- errmsg("invalid input syntax for type numeric: \"%s\"",
- str)));
+ (errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
+ errmsg("value overflows numeric format")));
dweight += (int) exponent;
dscale -= (int) exponent;
if (dscale < 0)
diff --git a/src/include/utils/numeric.h b/src/include/utils/numeric.h
index 6f68bfab2f7..48d5186d47c 100644
--- a/src/include/utils/numeric.h
+++ b/src/include/utils/numeric.h
@@ -17,8 +17,9 @@
#include "fmgr.h"
/*
- * Hardcoded precision limit - arbitrary, but must be small enough that
- * dscale values will fit in 14 bits.
+ * Limit on the precision (and hence scale) specifiable in a NUMERIC typmod.
+ * Note that the implementation limit on the length of a numeric value is
+ * much larger --- beware of what you use this for!
*/
#define NUMERIC_MAX_PRECISION 1000
diff --git a/src/interfaces/ecpg/pgtypeslib/numeric.c b/src/interfaces/ecpg/pgtypeslib/numeric.c
index 35eacf86503..d29170dffa0 100644
--- a/src/interfaces/ecpg/pgtypeslib/numeric.c
+++ b/src/interfaces/ecpg/pgtypeslib/numeric.c
@@ -263,8 +263,7 @@ set_var_from_str(char *str, char **ptr, numeric *dest)
return -1;
}
(*ptr) = endptr;
- if (exponent > NUMERIC_MAX_PRECISION ||
- exponent < -NUMERIC_MAX_PRECISION)
+ if (exponent >= INT_MAX / 2 || exponent <= -(INT_MAX / 2))
{
errno = PGTYPES_NUM_BAD_NUMERIC;
return -1;