Skip to content

Commit 9ef661d

Browse files
jianhe-funCommitfest Bot
authored andcommitted
error safe for casting character 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 ='character'::regtype order by castsource::regtype; castsource | casttarget | castfunc | castcontext | castmethod | prosrc | proname ------------+-------------------+----------+-------------+------------+-------------+--------- character | text | 401 | i | f | rtrim1 | text character | character varying | 401 | i | f | rtrim1 | text character | "char" | 944 | a | f | text_char | char character | name | 409 | i | f | bpchar_name | name character | xml | 2896 | e | f | texttoxml | xml character | character | 668 | i | f | bpchar | bpchar (6 rows) only texttoxml, bpchar(PG_FUNCTION_ARGS) need take care of error handling. other functions already error safe. discussion: https://postgr.es/m/CADkLM=fv1JfY4Ufa-jcwwNbjQixNViskQ8jZu3Tz_p656i_4hQ@mail.gmail.com
1 parent b978e27 commit 9ef661d

File tree

4 files changed

+15
-9
lines changed

4 files changed

+15
-9
lines changed

src/backend/executor/execExprInterp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4542,7 +4542,7 @@ ExecEvalXmlExpr(ExprState *state, ExprEvalStep *op)
45424542

45434543
*op->resvalue = PointerGetDatum(xmlparse(data,
45444544
xexpr->xmloption,
4545-
preserve_whitespace));
4545+
preserve_whitespace, NULL));
45464546
*op->resnull = false;
45474547
}
45484548
break;

src/backend/utils/adt/varchar.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -307,7 +307,7 @@ bpchar(PG_FUNCTION_ARGS)
307307
{
308308
for (i = maxmblen; i < len; i++)
309309
if (s[i] != ' ')
310-
ereport(ERROR,
310+
ereturn(fcinfo->context, (Datum) 0,
311311
(errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION),
312312
errmsg("value too long for type character(%d)",
313313
maxlen)));

src/backend/utils/adt/xml.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,7 @@ texttoxml(PG_FUNCTION_ARGS)
659659
{
660660
text *data = PG_GETARG_TEXT_PP(0);
661661

662-
PG_RETURN_XML_P(xmlparse(data, xmloption, true));
662+
PG_RETURN_XML_P(xmlparse(data, xmloption, true, fcinfo->context));
663663
}
664664

665665

@@ -1028,19 +1028,25 @@ xmlelement(XmlExpr *xexpr,
10281028

10291029

10301030
xmltype *
1031-
xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace)
1031+
xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, Node *escontext)
10321032
{
10331033
#ifdef USE_LIBXML
10341034
xmlDocPtr doc;
10351035

10361036
doc = xml_parse(data, xmloption_arg, preserve_whitespace,
1037-
GetDatabaseEncoding(), NULL, NULL, NULL);
1038-
xmlFreeDoc(doc);
1037+
GetDatabaseEncoding(), NULL, NULL, escontext);
1038+
if (doc)
1039+
xmlFreeDoc(doc);
1040+
1041+
if (SOFT_ERROR_OCCURRED(escontext))
1042+
return NULL;
10391043

10401044
return (xmltype *) data;
10411045
#else
1042-
NO_XML_SUPPORT();
1043-
return NULL;
1046+
ereturn(escontext, NULL
1047+
errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
1048+
errmsg("unsupported XML feature"),
1049+
errdetail("This functionality requires the server to be built with libxml support."));
10441050
#endif
10451051
}
10461052

src/include/utils/xml.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ extern xmltype *xmlconcat(List *args);
7373
extern xmltype *xmlelement(XmlExpr *xexpr,
7474
const Datum *named_argvalue, const bool *named_argnull,
7575
const Datum *argvalue, const bool *argnull);
76-
extern xmltype *xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace);
76+
extern xmltype *xmlparse(text *data, XmlOptionType xmloption_arg, bool preserve_whitespace, Node *escontext);
7777
extern xmltype *xmlpi(const char *target, text *arg, bool arg_is_null, bool *result_is_null);
7878
extern xmltype *xmlroot(xmltype *data, text *version, int standalone);
7979
extern bool xml_is_document(xmltype *arg);

0 commit comments

Comments
 (0)