Skip to content

Commit 0ea64a6

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting bytea 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 ='bytea'::regtype order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname ------------+------------+----------+-------------+------------+------------+--------- bytea | smallint | 6370 | e | f | bytea_int2 | int2 bytea | integer | 6371 | e | f | bytea_int4 | int4 bytea | bigint | 6372 | e | f | bytea_int8 | int8 (3 rows) discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent 65f4976 commit 0ea64a6

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

src/backend/utils/adt/bytea.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,10 +1027,14 @@ bytea_int2(PG_FUNCTION_ARGS)
10271027

10281028
/* Check that the byte array is not too long */
10291029
if (len > sizeof(result))
1030-
ereport(ERROR,
1030+
{
1031+
errsave(fcinfo->context,
10311032
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10321033
errmsg("smallint out of range"));
10331034

1035+
PG_RETURN_NULL();
1036+
}
1037+
10341038
/* Convert it to an integer; most significant bytes come first */
10351039
result = 0;
10361040
for (int i = 0; i < len; i++)
@@ -1052,10 +1056,14 @@ bytea_int4(PG_FUNCTION_ARGS)
10521056

10531057
/* Check that the byte array is not too long */
10541058
if (len > sizeof(result))
1055-
ereport(ERROR,
1059+
{
1060+
errsave(fcinfo->context,
10561061
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10571062
errmsg("integer out of range"));
10581063

1064+
PG_RETURN_NULL();
1065+
}
1066+
10591067
/* Convert it to an integer; most significant bytes come first */
10601068
result = 0;
10611069
for (int i = 0; i < len; i++)
@@ -1077,10 +1085,14 @@ bytea_int8(PG_FUNCTION_ARGS)
10771085

10781086
/* Check that the byte array is not too long */
10791087
if (len > sizeof(result))
1080-
ereport(ERROR,
1088+
{
1089+
errsave(fcinfo->context,
10811090
errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
10821091
errmsg("bigint out of range"));
10831092

1093+
PG_RETURN_NULL();
1094+
}
1095+
10841096
/* Convert it to an integer; most significant bytes come first */
10851097
result = 0;
10861098
for (int i = 0; i < len; i++)

0 commit comments

Comments
 (0)