Skip to content

Commit 26c4f25

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 b9bed07 commit 26c4f25

File tree

3 files changed

+32
-6
lines changed

3 files changed

+32
-6
lines changed

src/backend/utils/adt/float.c

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

12001200
result = (float4) num;
12011201
if (unlikely(isinf(result)) && !isinf(num))
1202-
float_overflow_error();
1202+
{
1203+
errsave(fcinfo->context,
1204+
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1205+
errmsg("value out of range: overflow"));
1206+
1207+
PG_RETURN_NULL();
1208+
}
1209+
12031210
if (unlikely(result == 0.0f) && num != 0.0)
1204-
float_underflow_error();
1211+
{
1212+
errsave(fcinfo->context,
1213+
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
1214+
errmsg("value out of range: underflow"));
1215+
1216+
PG_RETURN_NULL();
1217+
}
12051218

12061219
PG_RETURN_FLOAT4(result);
12071220
}
@@ -1224,10 +1237,14 @@ dtoi4(PG_FUNCTION_ARGS)
12241237

12251238
/* Range check */
12261239
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT32(num)))
1227-
ereport(ERROR,
1240+
{
1241+
errsave(fcinfo->context,
12281242
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
12291243
errmsg("integer out of range")));
12301244

1245+
PG_RETURN_NULL();
1246+
}
1247+
12311248
PG_RETURN_INT32((int32) num);
12321249
}
12331250

@@ -1249,10 +1266,14 @@ dtoi2(PG_FUNCTION_ARGS)
12491266

12501267
/* Range check */
12511268
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT16(num)))
1252-
ereport(ERROR,
1269+
{
1270+
errsave(fcinfo->context,
12531271
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
12541272
errmsg("smallint out of range")));
12551273

1274+
PG_RETURN_NULL();
1275+
}
1276+
12561277
PG_RETURN_INT16((int16) num);
12571278
}
12581279

src/backend/utils/adt/int8.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1268,10 +1268,14 @@ dtoi8(PG_FUNCTION_ARGS)
12681268

12691269
/* Range check */
12701270
if (unlikely(isnan(num) || !FLOAT8_FITS_IN_INT64(num)))
1271-
ereport(ERROR,
1271+
{
1272+
errsave(fcinfo->context,
12721273
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
12731274
errmsg("bigint out of range")));
12741275

1276+
PG_RETURN_NULL();
1277+
}
1278+
12751279
PG_RETURN_INT64((int64) num);
12761280
}
12771281

src/backend/utils/adt/numeric.c

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

45724572
/* Assume we need not worry about leading/trailing spaces */
4573-
(void) set_var_from_str(buf, buf, &result, &endptr, NULL);
4573+
if (!set_var_from_str(buf, buf, &result, &endptr, fcinfo->context))
4574+
PG_RETURN_NULL();
45744575

45754576
res = make_result(&result);
45764577

0 commit comments

Comments
 (0)