From: Tom Lane <tgl@sss.pgh.pa.us>
To: Tommaso Sala <tommaso.sala@cla-it.eu>
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #12053: Strange behavior for numeric types with unspecified precision-scale
Date: Fri, 28 Nov 2014 21:33:04 -0500
Message-ID: <25038.1417228384@sss.pgh.pa.us> (raw)
In-Reply-To: <20726.1417220254@sss.pgh.pa.us>
References: <20141125111529.15508.34881@wrigleys.postgresql.org>
<14914.1417017051@sss.pgh.pa.us>
<CAPPt9DkAgK9MbLtLJB8B16N=5jyF4+-4jo9v2jqLudd5bwdPFQ@mail.gmail.com>
<15613.1417121313@sss.pgh.pa.us>
<CAPPt9DmSArf59pK3GU3pJgm1mZRnkUFBbsYnJDYmoVO4L_r2=g@mail.gmail.com>
<20726.1417220254@sss.pgh.pa.us>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
I wrote:
> So the question is whether it's worth adding logic to numeric_recv
> to guard against bogus dscale values. I think that detecting this
> case would probably add a noticeable number of cycles to numeric_in.
> (We can't just count the number of digits received, since (a) trailing
> zeroes past dscale are OK, and (b) what we have at this point is
> base-10000 digits not base-10 digits...) I guess we're usually willing
> to expend cycles to guard against client error, so maybe we should
> do it here too.
I had originally been thinking of throwing an error if the presented
dscale was too small for the number of digits sent, but after some
reflection it seems like it'd be safer to just silently truncate the
extra digits away. If we throw an error it's likely to break applications
that are dependent on this buggy data adapter, and I'm not sure that the
users will thank us for that. Truncating the extra digits will make the
value actually match what it would've printed as, and if we grant that the
Devart folk did any testing of their code at all, they probably looked at
what was printed and thought that that was what they intended. That is,
I'm assuming that dscale = 2 means they only want 2 decimal places in
the value.
So I propose the attached patch, which requires only a minimal amount
of new code and is about as fast as we're going to get if we want to
check this issue at all. (Note: the apparently new error condition
for out-of-range dscale doesn't create a backwards compatibility hazard,
because make_result would've barfed anyway. This is just a more
to-the-point error message.)
regards, tom lane
--
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugsAttachments:check-dscale-in-numeric_recv.patch (text/x-diff, 1.5K) downloaddiff --git a/src/backend/utils/adt/numeric.c b/src/backend/utils/adt/numeric.cindex d61af92..c73f9bc 100644
*** a/src/backend/utils/adt/numeric.c
--- b/src/backend/utils/adt/numeric.c
*************** numeric_recv(PG_FUNCTION_ARGS)
*** 717,722 ****
--- 717,724 ----
alloc_var(&value, len);
value.weight = (int16) pq_getmsgint(buf, sizeof(int16));
+ /* we allow any int16 for weight --- OK? */+
value.sign = (uint16) pq_getmsgint(buf, sizeof(uint16));
if (!(value.sign == NUMERIC_POS ||
value.sign == NUMERIC_NEG ||
*************** numeric_recv(PG_FUNCTION_ARGS)
*** 726,731 ****
--- 728,738 ----
errmsg("invalid sign in external \"numeric\" value")));
value.dscale = (uint16) pq_getmsgint(buf, sizeof(uint16));
+ if ((value.dscale & NUMERIC_DSCALE_MASK) != value.dscale)+ ereport(ERROR,+ (errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),+ errmsg("invalid scale in external \"numeric\" value")));+
for (i = 0; i < len; i++)
{
NumericDigit d = pq_getmsgint(buf, sizeof(NumericDigit));
*************** numeric_recv(PG_FUNCTION_ARGS)
*** 737,742 ****
--- 744,757 ----
value.digits[i] = d;
}
+ /*+ * If the given dscale would hide any digits, truncate those digits away.+ * We could alternatively throw an error, but that would take a bunch of+ * extra code (about as much as trunc_var involves), and it might cause+ * client compatibility issues.+ */+ trunc_var(&value, value.dscale);+
apply_typmod(&value, typmod);
res = make_result(&value);
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: tgl@sss.pgh.pa.us, tommaso.sala@cla-it.eu
Subject: Re: BUG #12053: Strange behavior for numeric types with unspecified precision-scale
In-Reply-To: <25038.1417228384@sss.pgh.pa.us>
References: <20141125111529.15508.34881@wrigleys.postgresql.org> <14914.1417017051@sss.pgh.pa.us> <CAPPt9DkAgK9MbLtLJB8B16N=5jyF4+-4jo9v2jqLudd5bwdPFQ@mail.gmail.com> <15613.1417121313@sss.pgh.pa.us> <CAPPt9DmSArf59pK3GU3pJgm1mZRnkUFBbsYnJDYmoVO4L_r2=g@mail.gmail.com> <20726.1417220254@sss.pgh.pa.us> <25038.1417228384@sss.pgh.pa.us>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox