8000 Fix behavior when converting a float infinity to numeric. · etolbakov/postgres@def03e4 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit def03e4

Browse files
committed
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
1 parent af4bd07 commit def03e4

File tree

3 files changed

+41
-2
lines changed

3 files changed

+41
-2
lines changed

src/backend/utils/adt/numeric.c

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3023,7 +3023,12 @@ float8_numeric(PG_FUNCTION_ARGS)
30233023
if (isnan(val))
30243024
PG_RETURN_NUMERIC(make_result(&const_nan));
30253025

3026-
sprintf(buf, "%.*g", DBL_DIG, val);
3026+
if (isinf(val))
3027+
ereport(ERROR,
3028+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3029+
errmsg("cannot convert infinity to numeric")));
3030+
3031+
snprintf(buf, sizeof(buf), "%.*g", DBL_DIG, val);
30273032

30283033
init_var(&result);
30293034

@@ -3085,7 +3090,12 @@ float4_numeric(PG_FUNCTION_ARGS)
30853090
if (isnan(val))
30863091
PG_RETURN_NUMERIC(make_result(&const_nan));
30873092

3088-
sprintf(buf, "%.*g", FLT_DIG, val);
3093+
if (isinf(val))
3094+
ereport(ERROR,
3095+
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
3096+
errmsg("cannot convert infinity to numeric")));
3097+
3098+
snprintf(buf, sizeof(buf), "%.*g", FLT_DIG, val);
30893099

30903100
init_var(&result);
30913101

src/test/regress/expected/numeric.out

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,27 @@ SELECT * FROM fract_only;
708708
(6 rows)
709709

710710
DROP TABLE fract_only;
711+
-- Check inf/nan conversion behavior
712+
SELECT 'NaN'::float8::numeric;
713+
numeric
714+
---------
715+
NaN
716+
(1 row)
717+
718+
SELECT 'Infinity'::float8::numeric;
719+
ERROR: cannot convert infinity to numeric
720+
SELECT '-Infinity'::float8::numeric;
721+
ERROR: cannot convert infinity to numeric
722+
SELECT 'NaN'::float4::numeric;
723+
numeric
724+
---------
725+
NaN
726+
(1 row)
727+
728+
SELECT 'Infinity'::float4::numeric;
729+
ERROR: cannot convert infinity to numeric
730+
SELECT '-Infinity'::float4::numeric;
731+
ERROR: cannot convert infinity to numeric
711732
-- Simple check that ceil(), floor(), and round() work correctly
712733
CREATE TABLE ceil_floor_round (a numeric);
713734
INSERT INTO ceil_floor_round VALUES ('-5.5');

src/test/regress/sql/numeric.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -655,6 +655,14 @@ INSERT INTO fract_only VALUES (8, '0.00017');
655655
SELECT * FROM fract_only;
656656
DROP TABLE fract_only;
657657

658+
-- Check inf/nan conversion behavior
659+
SELECT 'NaN'::float8::numeric;
660+
SELECT 'Infinity'::float8::numeric;
661+
SELECT '-Infinity'::float8::numeric;
662+
SELECT 'NaN'::float4::numeric;
663+
SELECT 'Infinity'::float4::numeric;
664+
SELECT '-Infinity'::float4::numeric;
665+
658666
-- Simple check that ceil(), floor(), and round() work correctly
659667
CREATE TABLE ceil_floor_round (a numeric);
660668
INSERT INTO ceil_floor_round VALUES ('-5.5');

0 commit comments

Comments
 (0)
0