pgsql: Avoid overflow in width_bucket_float8().

Поиск
Список
Период
Сортировка
От Tom Lane
Тема pgsql: Avoid overflow in width_bucket_float8().
Дата
Msg-id E1phuBh-000UUD-DZ@gemulon.postgresql.org
обсуждение исходный текст
Список pgsql-committers
Avoid overflow in width_bucket_float8().

The original coding of this function paid little attention to the
possibility of overflow.  There were actually three different hazards:

1. The range from bound1 to bound2 could exceed DBL_MAX, which on
IEEE-compliant machines produces +Infinity in the subtraction.
At best we'd lose all precision in the result, and at worst
produce NaN due to dividing Inf/Inf.  The range can't exceed
twice DBL_MAX though, so we can fix this case by scaling all the
inputs by 0.5.

2. We computed count * (operand - bound1), which is also at risk of
float overflow, before dividing.  Safer is to do the division first,
producing a quotient that should be in [0,1), and even after allowing
for roundoff error can't be outside [0,1]; then multiplying by count
can't produce a result overflowing an int.  (width_bucket_numeric does
the multiplication first on the grounds that that improves accuracy of
its result, but I don't think that a similar argument can be made in
float arithmetic.)

3. If the division result does round to 1, and count is INT_MAX,
the final addition of 1 would overflow an int.  We took care
of that in the operand >= bound2 case but did not consider that
it could be possible in the main path.  Fix that by moving the
overflow-aware addition of 1 so it is done that way in all cases.

The fix for point 2 creates a possibility that values very close to
a bucket boundary will be rounded differently than they were before.
I'm not troubled by that for HEAD, but it is an argument against
putting this into the stable branches.  Given that the cases being
fixed here are fairly extreme and unlikely to be hit in normal use,
it seems best not to back-patch.

Mats Kindahl and Tom Lane

Discussion: https://postgr.es/m/17876-61f280d1601f978d@postgresql.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/b0e9e4d76ca212d429d9cd615ae62ac73a9a89ba

Modified Files
--------------
src/backend/utils/adt/float.c         | 43 +++++++++++++++++++++++------------
src/test/regress/expected/numeric.out | 39 +++++++++++++++++++++++++++++++
src/test/regress/sql/numeric.sql      | 22 ++++++++++++++++++
3 files changed, 89 insertions(+), 15 deletions(-)


В списке pgsql-committers по дате отправления:

Предыдущее
От: Daniel Gustafsson
Дата:
Сообщение: pgsql: Fix pointer cast for seed calculation on 32-bit systems
Следующее
От: Robert Haas
Дата:
Сообщение: pgsql: Add new predefined role pg_create_subscription.