pgsql: Fix power_var_int() for large integer exponents.

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: pgsql-committers(at)postgresql(dot)org
Subject: pgsql: Fix power_var_int() for large integer exponents.
Date: 2014-09-12 03:31:25
Message-ID: E1XSHa1-00086a-2A@gemulon.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Fix power_var_int() for large integer exponents.

The code for raising a NUMERIC value to an integer power wasn't very
careful about large powers. It got an outright wrong answer for an
exponent of INT_MIN, due to failure to consider overflow of the Abs(exp)
operation; which is fixable by using an unsigned rather than signed
exponent value after that point. Also, even though the number of
iterations of the power-computation loop is pretty limited, it's easy for
the repeated squarings to result in ridiculously enormous intermediate
values, which can take unreasonable amounts of time/memory to process,
or even overflow the internal "weight" field and so produce a wrong answer.
We can forestall misbehaviors of that sort by bailing out as soon as the
weight value exceeds what will fit in int16, since then the final answer
must overflow (if exp > 0) or underflow (if exp < 0) the packed numeric
format.

Per off-list report from Pavel Stehule. Back-patch to all supported
branches.

Branch
------
REL9_0_STABLE

Details
-------
http://git.postgresql.org/pg/commitdiff/26f8a4691e2ab459e856ba513aa559e7043d08fa

Modified Files
--------------
src/backend/utils/adt/numeric.c | 30 ++++++++++++++++++++++++++----
src/test/regress/expected/numeric.out | 19 +++++++++++++++++++
src/test/regress/sql/numeric.sql | 9 +++++++++
3 files changed, 54 insertions(+), 4 deletions(-)

Browse pgsql-committers by date

  From Date Subject
Next Message Heikki Linnakangas 2014-09-12 08:39:03 pgsql: Fix GIN data page split ratio calculation.
Previous Message Stephen Frost 2014-09-12 02:41:16 Re: pgsql: Fix JSON regression tests.