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

Commit6356512

Browse files
committed
Remove bogus dependencies on NUMERIC_MAX_PRECISION.
NUMERIC_MAX_PRECISION is a purely arbitrary constraint on the precisionand scale you can write in a numeric typmod. It might once have hadsomething to do with the allowed range of a typmod-less numeric value,but at least since 9.1 we've allowed, and documented that we allowed,any value that would physically fit in the numeric storage format;which is something over 100000 decimal digits, not 1000.Hence, get rid of numeric_in()'s use of NUMERIC_MAX_PRECISION as a limiton the allowed range of the exponent in scientific-format input. That wasespecially silly in view of the fact that you can enter larger numbers aslong as you don't use 'e' to do it. Just constrain the value enough toavoid localized overflow, and let make_result be the final arbiter of whatis too large. Likewise adjust ecpg's equivalent of this code.Also get rid of numeric_recv()'s use of NUMERIC_MAX_PRECISION to limit thenumber of base-NBASE digits it would accept. That created a dump/restorehazard for binary COPY without doing anything useful; the wire-formatlimit on number of digits (65535) is about as tight as we would want.In HEAD, also get rid of pg_size_bytes()'s unnecessary intimacy with whatthe numeric range limit is. That code doesn't exist in the back branches.Per gripe from Aravind Kumar. Back-patch to all supported branches,since they all contain the documentation claim about allowed range ofNUMERIC (cf commitcabf5d8).Discussion: <2895.1471195721@sss.pgh.pa.us>
1 parented2d7b8 commit6356512

File tree

3 files changed

+16
-13
lines changed

3 files changed

+16
-13
lines changed

‎src/backend/utils/adt/numeric.c

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -760,10 +760,6 @@ numeric_recv(PG_FUNCTION_ARGS)
760760
init_var(&value);
761761

762762
len= (uint16)pq_getmsgint(buf,sizeof(uint16));
763-
if (len<0||len>NUMERIC_MAX_PRECISION+NUMERIC_MAX_RESULT_SCALE)
764-
ereport(ERROR,
765-
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
766-
errmsg("invalid length in external \"numeric\" value")));
767763

768764
alloc_var(&value,len);
769765

@@ -4651,12 +4647,19 @@ set_var_from_str(const char *str, const char *cp, NumericVar *dest)
46514647
errmsg("invalid input syntax for type numeric: \"%s\"",
46524648
str)));
46534649
cp=endptr;
4654-
if (exponent>NUMERIC_MAX_PRECISION||
4655-
exponent<-NUMERIC_MAX_PRECISION)
4650+
4651+
/*
4652+
* At this point, dweight and dscale can't be more than about
4653+
* INT_MAX/2 due to the MaxAllocSize limit on string length, so
4654+
* constraining the exponent similarly should be enough to prevent
4655+
* integer overflow in this function. If the value is too large to
4656+
* fit in storage format, make_result() will complain about it later;
4657+
* for consistency use the same ereport errcode/text as make_result().
4658+
*/
4659+
if (exponent >=INT_MAX /2||exponent <=-(INT_MAX /2))
46564660
ereport(ERROR,
4657-
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
4658-
errmsg("invalid input syntax for type numeric: \"%s\"",
4659-
str)));
4661+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
4662+
errmsg("value overflows numeric format")));
46604663
dweight+= (int)exponent;
46614664
dscale-= (int)exponent;
46624665
if (dscale<0)

‎src/include/utils/numeric.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,9 @@
1717
#include"fmgr.h"
1818

1919
/*
20-
* Hardcoded precision limit - arbitrary, but must be small enough that
21-
* dscale values will fit in 14 bits.
20+
* Limit on the precision (and hence scale) specifiable in a NUMERIC typmod.
21+
* Note that the implementation limit on the length of a numeric value is
22+
* much larger --- beware of what you use this for!
2223
*/
2324
#defineNUMERIC_MAX_PRECISION1000
2425

‎src/interfaces/ecpg/pgtypeslib/numeric.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,8 +263,7 @@ set_var_from_str(char *str, char **ptr, numeric *dest)
263263
return-1;
264264
}
265265
(*ptr)=endptr;
266-
if (exponent>NUMERIC_MAX_PRECISION||
267-
exponent<-NUMERIC_MAX_PRECISION)
266+
if (exponent >=INT_MAX /2||exponent <=-(INT_MAX /2))
268267
{
269268
errno=PGTYPES_NUM_BAD_NUMERIC;
270269
return-1;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp