Bug in numeric_power() if exponent is INT_MIN

From: Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
To: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Subject: Bug in numeric_power() if exponent is INT_MIN
Date: 2021-01-04 17:24:08
Message-ID: CAEZATCVd6pMkz=BrZEgBKyqqJrt2xghr=fNc8+Z=5xC6cgWrWA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

(Amusingly I only found this after discovering that Windows Calculator
has a similar bug which causes it to crash if you try to raise a
number to the power INT_MIN.)

On my machine, numeric_power() loses all precision if the exponent is
INT_MIN, though the actual failure mode might well be
platform-dependent:

SELECT n, 1.000000000123^n AS pow
FROM (VALUES (-2147483647), (-2147483648), (-2147483649)) AS v(n);
n | pow
-------------+--------------------
-2147483647 | 0.7678656557347558
-2147483648 | 1.0000000000000000
-2147483649 | 0.7678656555458609
(3 rows)

The issue is in this line from power_var_int():

sig_digits += (int) log(Abs(exp)) + 8;

because "exp" is a signed int, so Abs(exp) leaves INT_MIN unchanged.
The most straightforward fix is to use fabs() instead, so that "exp"
is cast to double *before* the absolute value is taken, as in the
attached patch.

This code was added in 7d9a4737c2, which first appeared in PG 9.6, so
barring objections, I'll push and back-patch this fix that far.

Regards,
Dean

Attachment Content-Type Size
fix-numeric-pow-for-int-min-exponent.patch text/x-patch 1.4 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Zhihong Yu 2021-01-04 17:38:02 Re: poc - possibility to write window function in PL languages
Previous Message Justin Pryzby 2021-01-04 17:09:39 [PATCH]: Allow errors in parameter values to be reported during the BIND phase itself..