]> granicus.if.org Git - postgresql/commit
Fix behavior when converting a float infinity to numeric.
authorTom Lane <tgl@sss.pgh.pa.us>
Wed, 27 Sep 2017 21:05:54 +0000 (17:05 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Wed, 27 Sep 2017 21:05:54 +0000 (17:05 -0400)
commit2e82fba0e6a9f5d3253b89e5f2de2b1bf042d71c
tree7c9e6c78700c623104b7951f610e85323f77e0ea
parent43661926deb7d412ae3a22a43dadb9def7b7e46c
Fix behavior when converting a float infinity to numeric.

float8_numeric() and float4_numeric() failed to consider the possibility
that the input is an IEEE infinity.  The results depended on the
platform-specific behavior of sprintf(): on most platforms you'd get
something like

ERROR:  invalid input syntax for type numeric: "inf"

but at least on Windows it's possible for the conversion to succeed and
deliver a finite value (typically 1), due to a nonstandard output format
from sprintf and lack of syntax error checking in these functions.

Since our numeric type lacks the concept of infinity, a suitable conversion
is impossible; the best thing to do is throw an explicit error before
letting sprintf do its thing.

While at it, let's use snprintf not sprintf.  Overrunning the buffer
should be impossible if sprintf does what it's supposed to, but this
is cheap insurance against a stack smash if it doesn't.

Problem reported by Taiki Kondo.  Patch by me based on fix suggestion
from KaiGai Kohei.  Back-patch to all supported branches.

Discussion: https://postgr.es/m/12A9442FBAE80D4E8953883E0B84E088C8C7A2@BPXM01GP.gisp.nec.co.jp
src/backend/utils/adt/numeric.c
src/test/regress/expected/numeric.out
src/test/regress/sql/numeric.sql