diff --git a/src/backend/parser/parse_coerce.c b/src/backend/parser/parse_coerce.c index 79ffdd2a9d1..a812f4224c9 100644 --- a/src/backend/parser/parse_coerce.c +++ b/src/backend/parser/parse_coerce.c @@ -571,6 +571,10 @@ can_coerce_type(int nargs, const Oid *input_typeids, const Oid *target_typeids, if (inputTypeId == targetTypeId) continue; + /* reject all cases of casting something else to/from "internal" */ + if (inputTypeId == INTERNALOID || targetTypeId == INTERNALOID) + return false; + /* accept if target is ANY */ if (targetTypeId == ANYOID) continue; @@ -3150,6 +3154,10 @@ find_coercion_pathway(Oid targetTypeId, Oid sourceTypeId, if (sourceTypeId == targetTypeId) return COERCION_PATH_RELABELTYPE; + /* Reject all cases of casting something else to/from "internal" */ + if (sourceTypeId == INTERNALOID || targetTypeId == INTERNALOID) + return COERCION_PATH_NONE; + /* Look in pg_cast */ tuple = SearchSysCache2(CASTSOURCETARGET, ObjectIdGetDatum(sourceTypeId), diff --git a/src/backend/parser/parse_func.c b/src/backend/parser/parse_func.c index a644a6f5e1e..62b42319516 100644 --- a/src/backend/parser/parse_func.c +++ b/src/backend/parser/parse_func.c @@ -672,6 +672,32 @@ ParseFuncOrColumn(ParseState *pstate, List *funcname, List *fargs, rettype, false); + /* + * Reject any attempt to call a function that takes or returns type + * internal from SQL. (The FUNCDETAIL_COERCION case does not reach this + * check because of the early return above, but that's okay because we + * disallow coercions to or from type internal.) Note that we are + * checking the resolved argument and result types, so this will reject + * calls to polymorphic functions that pass internal-type arguments. The + * casting rules should prevent that anyway, since we won't cast internal + * to any polymorphic type, but no harm in being doubly sure. + */ + for (int i = 0; i < nargsplusdefs; i++) + { + if (declared_arg_types[i] == INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("functions accepting type \"%s\" cannot be called explicitly", + "internal"), + parser_errposition(pstate, location))); + } + if (rettype == INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("functions returning type \"%s\" cannot be called explicitly", + "internal"), + parser_errposition(pstate, location))); + /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, fargs, actual_arg_types, declared_arg_types); diff --git a/src/backend/parser/parse_oper.c b/src/backend/parser/parse_oper.c index 4e460799903..adc1e031ee4 100644 --- a/src/backend/parser/parse_oper.c +++ b/src/backend/parser/parse_oper.c @@ -749,6 +749,28 @@ make_op(ParseState *pstate, List *opname, Node *ltree, Node *rtree, opform->oprresult, false); + /* + * Reject any attempt to call a function that takes or returns type + * internal from SQL. This is just like the check in ParseFuncOrColumn, + * but for operator syntax. (Despite that, we say "function" in the error + * messages; doesn't seem worth having two sets of translatable strings.) + */ + for (int i = 0; i < nargs; i++) + { + if (declared_arg_types[i] == INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("functions accepting type \"%s\" cannot be called explicitly", + "internal"), + parser_errposition(pstate, location))); + } + if (rettype == INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), + errmsg("functions returning type \"%s\" cannot be called explicitly", + "internal"), + parser_errposition(pstate, location))); + /* perform the necessary typecasting of arguments */ make_fn_arguments(pstate, args, actual_arg_types, declared_arg_types); diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index b02a1b93b3f..438c349b7a6 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -7829,13 +7829,21 @@ get_cast_hashentry(PLpgSQL_execstate *estate, * If there's no cast path according to the parser, fall back to using * an I/O coercion; this is semantically dubious but matches plpgsql's * historical behavior. We would need something of the sort for - * UNKNOWN literals in any case. (This is probably now only reachable - * in the case where srctype is UNKNOWN/RECORD.) + * UNKNOWN literals in any case. The only case we reject is casting + * to/from INTERNAL. (Other than that case, this is probably now only + * reachable in the case where srctype is UNKNOWN/RECORD.) */ if (cast_expr == NULL) { CoerceViaIO *iocoerce = makeNode(CoerceViaIO); + if (srctype == INTERNALOID || dsttype == INTERNALOID) + ereport(ERROR, + (errcode(ERRCODE_CANNOT_COERCE), + errmsg("cannot cast type %s to %s", + format_type_be(srctype), + format_type_be(dsttype)))); + iocoerce->arg = (Expr *) placeholder; iocoerce->resulttype = dsttype; iocoerce->resultcollid = InvalidOid;