]> granicus.if.org Git - postgresql/commit
Fix power_var_int() for large integer exponents.
authorTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Sep 2014 03:30:57 +0000 (23:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Sep 2014 03:30:57 +0000 (23:30 -0400)
commit25bf13fe1b5e9385ec230e457d488684e5933a32
treee98e1492614b2677abc0e25cccc4df60b0a9acb1
parent5724f491d2e02d6017e3101c9b7421437f2129e5
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.
src/backend/utils/adt/numeric.c
src/test/regress/expected/numeric.out
src/test/regress/sql/numeric.sql