Skip to content

Commit ca83963

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting float8 to other types per pg_cast
select castsource::regtype, casttarget::regtype, castfunc, castcontext,castmethod, pp.prosrc, pp.proname from pg_cast pc join pg_proc pp on pp.oid = pc.castfunc and pc.castfunc > 0 and castsource::regtype = 'float8'::regtype order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname ------------------+------------+----------+-------------+------------+----------------+--------- double precision | bigint | 483 | a | f | dtoi8 | int8 double precision | smallint | 237 | a | f | dtoi2 | int2 double precision | integer | 317 | a | f | dtoi4 | int4 double precision | real | 312 | a | f | dtof | float4 double precision | numeric | 1743 | a | f | float8_numeric | numeric (5 rows) discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent 50d4a4d commit ca83963

File tree

3 files changed

+12
-6
lines changed

3 files changed

+12
-6
lines changed

src/backend/utils/adt/float.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1199,9 +1199,14 @@ dtof(PG_FUNCTION_ARGS)
11991199

12001200
result = (float4) num;
12011201
if (unlikely(isinf(result)) && !isinf(num))
1202-
float_overflow_error();
1202+
ereturn(fcinfo->context, (Datum) 0,
1203+
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1204+
errmsg("value out of range: overflow"));
1205+
12031206
if (unlikely(result == 0.0f) && num != 0.0)
1204-
float_underflow_error();
1207+
ereturn(fcinfo->context, (Datum) 0,
1208+
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1209+
errmsg("value out of range: underflow"));
12051210

12061211
PG_RETURN_FLOAT4(result);
12071212
}
@@ -1224,7 +1229,7 @@ dtoi4(PG_FUNCTION_ARGS)
12241229

12251230
/* Range check */
12261231
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT32(num)))
1227-
ereport(ERROR,
1232+
ereturn(fcinfo->context, (Datum) 0,
12281233
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
12291234
errmsg("integer out of range")));
12301235

@@ -1249,7 +1254,7 @@ dtoi2(PG_FUNCTION_ARGS)
12491254

12501255
/* Range check */
12511256
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT16(num)))
1252-
ereport(ERROR,
1257+
ereturn(fcinfo->context, (Datum) 0,
12531258
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
12541259
errmsg("smallint out of range")));
12551260

src/backend/utils/adt/int8.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1307,7 +1307,7 @@ dtoi8(PG_FUNCTION_ARGS)
13071307

13081308
/* Range check */
13091309
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
1310-
ereport(ERROR,
1310+
ereturn(fcinfo->context, (Datum) 0,
13111311
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
13121312
errmsg("bigint out of range")));
13131313

src/backend/utils/adt/numeric.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4560,7 +4560,8 @@ float8_numeric(PG_FUNCTION_ARGS)
45604560
init_var(&result);
45614561

45624562
/* Assume we need not worry about leading/trailing spaces */
4563-
(void) set_var_from_str(buf, buf, &result, &endptr, NULL);
4563+
if (!set_var_from_str(buf, buf, &result, &endptr, fcinfo->context))
4564+
PG_RETURN_NULL();
45644565

45654566
res = make_result(&result);
45664567

0 commit comments

Comments
 (0)