]> granicus.if.org Git - postgresql/commit
Fix float-to-integer coercions to handle edge cases correctly.
authorTom Lane <tgl@sss.pgh.pa.us>
Sat, 24 Nov 2018 17:45:50 +0000 (12:45 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Sat, 24 Nov 2018 17:45:50 +0000 (12:45 -0500)
commit93eec1238663beab8521605f08a5937dbd8ec114
treee5f32dda3da8f23ce1cb0745c35a7b65d6128611
parent5f11a500fac69da4c3de61bf878d66f73f4fc8d3
Fix float-to-integer coercions to handle edge cases correctly.

ftoi4 and its sibling coercion functions did their overflow checks in
a way that looked superficially plausible, but actually depended on an
assumption that the MIN and MAX comparison constants can be represented
exactly in the float4 or float8 domain.  That fails in ftoi4, ftoi8,
and dtoi8, resulting in a possibility that values near the MAX limit will
be wrongly converted (to negative values) when they need to be rejected.

Also, because we compared before rounding off the fractional part,
the other three functions threw errors for values that really ought
to get rounded to the min or max integer value.

Fix by doing rint() first (requiring an assumption that it handles
NaN and Inf correctly; but dtoi8 and ftoi8 were assuming that already),
and by comparing to values that should coerce to float exactly, namely
INTxx_MIN and -INTxx_MIN.  Also remove some random cosmetic discrepancies
between these six functions.

This back-patches commits cbdb8b4c0 and 452b637d4.  In the 9.4 branch,
also back-patch the portion of 62e2a8dc2 that added PG_INTnn_MIN and
related constants to c.h, so that these functions can rely on them.

Per bug #15519 from Victor Petrovykh.

Patch by me; thanks to Andrew Gierth for analysis and discussion.

Discussion: https://postgr.es/m/15519-4fc785b483201ff1@postgresql.org
src/backend/utils/adt/float.c
src/backend/utils/adt/int8.c
src/test/regress/expected/float4.out
src/test/regress/expected/float8-small-is-zero.out
src/test/regress/expected/float8.out
src/test/regress/sql/float4.sql
src/test/regress/sql/float8.sql