]> 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:54 +0000 (23:30 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Fri, 12 Sep 2014 03:30:54 +0000 (23:30 -0400)
commit1c603198a497173b4947fc669fdeda4706b09eb8
tree356227cb0ffa622814ab8a8c90a5c8b95abe4330
parent330bf85c4936fa6f0b59d4baad54cd8e1ce11604
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