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

Commitcf5c20b

Browse files
committed
Fix power_var_int() for large integer exponents.
The code for raising a NUMERIC value to an integer power wasn't verycareful about large powers. It got an outright wrong answer for anexponent of INT_MIN, due to failure to consider overflow of the Abs(exp)operation; which is fixable by using an unsigned rather than signedexponent value after that point. Also, even though the number ofiterations of the power-computation loop is pretty limited, it's easy forthe repeated squarings to result in ridiculously enormous intermediatevalues, 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 theweight value exceeds what will fit in int16, since then the final answermust overflow (if exp > 0) or underflow (if exp < 0) the packed numericformat.Per off-list report from Pavel Stehule. Back-patch to all supportedbranches.
1 parent7288331 commitcf5c20b

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5649,10 +5649,12 @@ power_var(NumericVar *base, NumericVar *exp, NumericVar *result)
56495649
static void
56505650
power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
56515651
{
5652+
unsigned int mask;
56525653
boolneg;
56535654
NumericVarbase_prod;
56545655
intlocal_rscale;
56555656

5657+
/* Handle some common special cases, as well as corner cases */
56565658
switch (exp)
56575659
{
56585660
case 0:
@@ -5686,23 +5688,43 @@ power_var_int(NumericVar *base, int exp, NumericVar *result, int rscale)
56865688
* pattern of exp. We do the multiplications with some extra precision.
56875689
*/
56885690
neg = (exp < 0);
5689-
exp = Abs(exp);
5691+
mask = Abs(exp);
56905692

56915693
local_rscale = rscale + MUL_GUARD_DIGITS * 2;
56925694

56935695
init_var(&base_prod);
56945696
set_var_from_var(base, &base_prod);
56955697

5696-
if (exp & 1)
5698+
if (mask & 1)
56975699
set_var_from_var(base, result);
56985700
else
56995701
set_var_from_var(&const_one, result);
57005702

5701-
while ((exp >>= 1) > 0)
5703+
while ((mask >>= 1) > 0)
57025704
{
57035705
mul_var(&base_prod, &base_prod, &base_prod, local_rscale);
5704-
if (exp & 1)
5706+
if (mask & 1)
57055707
mul_var(&base_prod, result, result, local_rscale);
5708+
5709+
/*
5710+
* When abs(base) > 1, the number of digits to the left of the decimal
5711+
* point in base_prod doubles at each iteration, so if exp is large we
5712+
* could easily spend large amounts of time and memory space doing the
5713+
* multiplications. But once the weight exceeds what will fit in
5714+
* int16, the final result is guaranteed to overflow (or underflow, if
5715+
* exp < 0), so we can give up before wasting too many cycles.
5716+
*/
5717+
if (base_prod.weight > SHRT_MAX || result->weight > SHRT_MAX)
5718+
{
5719+
/* overflow, unless neg, in which case result should be 0 */
5720+
if (!neg)
5721+
ereport(ERROR,
5722+
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
5723+
errmsg("value overflows numeric format")));
5724+
zero_var(result);
5725+
neg = false;
5726+
break;
5727+
}
57065728
}
57075729

57085730
free_var(&base_prod);

‎src/test/regress/expected/numeric.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,3 +1390,22 @@ select div(12345678901234567890, 123) * 123 + 12345678901234567890 % 123;
13901390
12345678901234567890
13911391
(1 row)
13921392

1393+
--
1394+
-- Test code path for raising to integer powers
1395+
--
1396+
select 10.0 ^ -2147483648 as rounds_to_zero;
1397+
rounds_to_zero
1398+
--------------------
1399+
0.0000000000000000
1400+
(1 row)
1401+
1402+
select 10.0 ^ -2147483647 as rounds_to_zero;
1403+
rounds_to_zero
1404+
--------------------
1405+
0.0000000000000000
1406+
(1 row)
1407+
1408+
select 10.0 ^ 2147483647 as overflows;
1409+
ERROR: value overflows numeric format
1410+
select 117743296169.0 ^ 1000000000 as overflows;
1411+
ERROR: value overflows numeric format

‎src/test/regress/sql/numeric.sql

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -828,3 +828,12 @@ select 12345678901234567890 % 123;
828828
select 12345678901234567890 / 123;
829829
select div(12345678901234567890, 123);
830830
select div(12345678901234567890, 123) * 123 + 12345678901234567890 % 123;
831+
832+
--
833+
-- Test code path for raising to integer powers
834+
--
835+
836+
select 10.0 ^ -2147483648 as rounds_to_zero;
837+
select 10.0 ^ -2147483647 as rounds_to_zero;
838+
select 10.0 ^ 2147483647 as overflows;
839+
select 117743296169.0 ^ 1000000000 as overflows;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp