Skip to content

Commit d843b0f

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting integer 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 = 'integer'::regtype order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname ------------+------------------+----------+-------------+------------+--------------+--------- integer | bigint | 481 | i | f | int48 | int8 integer | smallint | 314 | a | f | i4toi2 | int2 integer | real | 318 | i | f | i4tof | float4 integer | double precision | 316 | i | f | i4tod | float8 integer | numeric | 1740 | i | f | int4_numeric | numeric integer | money | 3811 | a | f | int4_cash | money integer | boolean | 2557 | e | f | int4_bool | bool integer | bytea | 6368 | e | f | int4_bytea | bytea integer | "char" | 78 | e | f | i4tochar | char integer | bit | 1683 | e | f | bitfromint4 | bit (10 rows) only int4_cash, i4toi2, i4tochar need take care of error handling. but support for cash data type is not easy, so only i4toi2, i4tochar function refactoring. discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent e74312f commit d843b0f

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/backend/utils/adt/char.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,10 +192,14 @@ i4tochar(PG_FUNCTION_ARGS)
192192
int32 arg1 = PG_GETARG_INT32(0);
193193

194194
if (arg1 < SCHAR_MIN || arg1 > SCHAR_MAX)
195-
ereport(ERROR,
195+
{
196+
errsave(fcinfo->context,
196197
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
197198
errmsg("\"char\" out of range")));
198199

200+
PG_RETURN_NULL();
201+
}
202+
199203
PG_RETURN_CHAR((int8) arg1);
200204
}
201205

src/backend/utils/adt/int.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -350,10 +350,13 @@ i4toi2(PG_FUNCTION_ARGS)
350350
int32 arg1 = PG_GETARG_INT32(0);
351351

352352
if (unlikely(arg1 < SHRT_MIN) || unlikely(arg1 > SHRT_MAX))
353-
ereport(ERROR,
353+
{
354+
errsave(fcinfo->context,
354355
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
355356
errmsg("smallint out of range")));
356357

358+
PG_RETURN_NULL();
359+
}
357360
PG_RETURN_INT16((int16) arg1);
358361
}
359362

0 commit comments

Comments
 (0)