Skip to content

Commit 3f9c882

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting bit/varbit 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 where pc.castfunc > 0 and (castsource::regtype ='bit'::regtype or castsource::regtype ='varbit'::regtype) order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname -------------+-------------+----------+-------------+------------+-----------+--------- bit | bigint | 2076 | e | f | bittoint8 | int8 bit | integer | 1684 | e | f | bittoint4 | int4 bit | bit | 1685 | i | f | bit | bit bit varying | bit varying | 1687 | i | f | varbit | varbit (4 rows) discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent 0ea64a6 commit 3f9c882

File tree

1 file changed

+20
-4
lines changed

1 file changed

+20
-4
lines changed

src/backend/utils/adt/varbit.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -401,11 +401,15 @@ bit(PG_FUNCTION_ARGS)
401401
PG_RETURN_VARBIT_P(arg);
402402

403403
if (!isExplicit)
404-
ereport(ERROR,
404+
{
405+
errsave(fcinfo->context,
405406
(errcode(ERRCODE_STRING_DATA_LENGTH_MISMATCH),
406407
errmsg("bit string length %d does not match type bit(%d)",
407408
VARBITLEN(arg), len)));
408409

410+
PG_RETURN_NULL();
411+
}
412+
409413
rlen = VARBITTOTALLEN(len);
410414
/* set to 0 so that string is zero-padded */
411415
result = (VarBit *) palloc0(rlen);
@@ -752,11 +756,15 @@ varbit(PG_FUNCTION_ARGS)
752756
PG_RETURN_VARBIT_P(arg);
753757

754758
if (!isExplicit)
755-
ereport(ERROR,
759+
{
760+
errsave(fcinfo->context,
756761
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
757762
errmsg("bit string too long for type bit varying(%d)",
758763
len)));
759764

765+
PG_RETURN_NULL();
766+
}
767+
760768
rlen = VARBITTOTALLEN(len);
761769
result = (VarBit *) palloc(rlen);
762770
SET_VARSIZE(result, rlen);
@@ -1591,10 +1599,14 @@ bittoint4(PG_FUNCTION_ARGS)
15911599

15921600
/* Check that the bit string is not too long */
15931601
if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1594-
ereport(ERROR,
1602+
{
1603+
errsave(fcinfo->context,
15951604
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
15961605
errmsg("integer out of range")));
15971606

1607+
PG_RETURN_NULL();
1608+
}
1609+
15981610
result = 0;
15991611
for (r = VARBITS(arg); r < VARBITEND(arg); r++)
16001612
{
@@ -1671,10 +1683,14 @@ bittoint8(PG_FUNCTION_ARGS)
16711683

16721684
/* Check that the bit string is not too long */
16731685
if (VARBITLEN(arg) > sizeof(result) * BITS_PER_BYTE)
1674-
ereport(ERROR,
1686+
{
1687+
errsave(fcinfo->context,
16751688
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
16761689
errmsg("bigint out of range")));
16771690

1691+
PG_RETURN_NULL();
1692+
}
1693+
16781694
result = 0;
16791695
for (r = VARBITS(arg); r < VARBITEND(arg); r++)
16801696
{

0 commit comments

Comments
 (0)