summaryrefslogtreecommitdiff
path: root/src/backend
diff options
context:
space:
mode:
authorTom Lane2022-12-14 22:50:24 +0000
committerTom Lane2022-12-14 22:50:24 +0000
commit47f3f97fcdee28e3eb70cd2ebfd7b4899570b018 (patch)
tree443f2edbb1c19fe1925f87f30e0edf66bc557b30 /src/backend
parent332741e73980401895e027eb697bb472860036fb (diff)
Convert a few more datatype input functions to report errors softly.
Convert assorted internal-ish datatypes, namely aclitemin, int2vectorin, oidin, oidvectorin, pg_lsn_in, pg_snapshot_in, and tidin to the new style. (Some others you might expect to find in this group, such as cidin and xidin, need no changes because they never throw errors at all. That seems a little cheesy ... but it is not in the charter of this patch series to add new error conditions.) Amul Sul, minor mods by me Discussion: https://postgr.es/m/CAAJ_b97KeDWUdpTKGOaFYPv0OicjOu6EW+QYWj-Ywrgj_aEy1g@mail.gmail.com
Diffstat (limited to 'src/backend')
-rw-r--r--src/backend/utils/adt/acl.c62
-rw-r--r--src/backend/utils/adt/int.c9
-rw-r--r--src/backend/utils/adt/oid.c37
-rw-r--r--src/backend/utils/adt/pg_lsn.c2
-rw-r--r--src/backend/utils/adt/tid.c9
-rw-r--r--src/backend/utils/adt/xid8funcs.c7
6 files changed, 85 insertions, 41 deletions
diff --git a/src/backend/utils/adt/acl.c b/src/backend/utils/adt/acl.c
index bba953cd6e0..d70b64a8cfe 100644
--- a/src/backend/utils/adt/acl.c
+++ b/src/backend/utils/adt/acl.c
@@ -81,11 +81,11 @@ static List *cached_roles[] = {NIL, NIL, NIL};
static uint32 cached_db_hash;
-static const char *getid(const char *s, char *n);
+static const char *getid(const char *s, char *n, Node *escontext);
static void putid(char *p, const char *s);
static Acl *allocacl(int n);
static void check_acl(const Acl *acl);
-static const char *aclparse(const char *s, AclItem *aip);
+static const char *aclparse(const char *s, AclItem *aip, Node *escontext);
static bool aclitem_match(const AclItem *a1, const AclItem *a2);
static int aclitemComparator(const void *arg1, const void *arg2);
static void check_circularity(const Acl *old_acl, const AclItem *mod_aip,
@@ -135,9 +135,12 @@ static void RoleMembershipCacheCallback(Datum arg, int cacheid, uint32 hashvalue
* in 's', after any quotes. Also:
* - loads the identifier into 'n'. (If no identifier is found, 'n'
* contains an empty string.) 'n' must be NAMEDATALEN bytes.
+ *
+ * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
+ * in which case we log the error there and return NULL.
*/
static const char *
-getid(const char *s, char *n)
+getid(const char *s, char *n, Node *escontext)
{
int len = 0;
bool in_quotes = false;
@@ -169,7 +172,7 @@ getid(const char *s, char *n)
/* Add the character to the string */
if (len >= NAMEDATALEN - 1)
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_NAME_TOO_LONG),
errmsg("identifier too long"),
errdetail("Identifier must be less than %d characters.",
@@ -236,9 +239,12 @@ putid(char *p, const char *s)
* specification. Also:
* - loads the structure pointed to by 'aip' with the appropriate
* UID/GID, id type identifier and mode type values.
+ *
+ * Errors are reported via ereport, unless escontext is an ErrorSaveData node,
+ * in which case we log the error there and return NULL.
*/
static const char *
-aclparse(const char *s, AclItem *aip)
+aclparse(const char *s, AclItem *aip, Node *escontext)
{
AclMode privs,
goption,
@@ -248,25 +254,30 @@ aclparse(const char *s, AclItem *aip)
Assert(s && aip);
- s = getid(s, name);
+ s = getid(s, name, escontext);
+ if (s == NULL)
+ return NULL;
if (*s != '=')
{
/* we just read a keyword, not a name */
if (strcmp(name, "group") != 0 && strcmp(name, "user") != 0)
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("unrecognized key word: \"%s\"", name),
errhint("ACL key word must be \"group\" or \"user\".")));
- s = getid(s, name); /* move s to the name beyond the keyword */
+ /* move s to the name beyond the keyword */
+ s = getid(s, name, escontext);
+ if (s == NULL)
+ return NULL;
if (name[0] == '\0')
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("missing name"),
errhint("A name must follow the \"group\" or \"user\" key word.")));
}
if (*s != '=')
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("missing \"=\" sign")));
@@ -328,7 +339,7 @@ aclparse(const char *s, AclItem *aip)
read = 0;
break;
default:
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid mode character: must be one of \"%s\"",
ACL_ALL_RIGHTS_STR)));
@@ -340,7 +351,13 @@ aclparse(const char *s, AclItem *aip)
if (name[0] == '\0')
aip->ai_grantee = ACL_ID_PUBLIC;
else
- aip->ai_grantee = get_role_oid(name, false);
+ {
+ aip->ai_grantee = get_role_oid(name, true);
+ if (!OidIsValid(aip->ai_grantee))
+ ereturn(escontext, NULL,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("role \"%s\" does not exist", name)));
+ }
/*
* XXX Allow a degree of backward compatibility by defaulting the grantor
@@ -348,12 +365,18 @@ aclparse(const char *s, AclItem *aip)
*/
if (*s == '/')
{
- s = getid(s + 1, name2);
+ s = getid(s + 1, name2, escontext);
+ if (s == NULL)
+ return NULL;
if (name2[0] == '\0')
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("a name must follow the \"/\" sign")));
- aip->ai_grantor = get_role_oid(name2, false);
+ aip->ai_grantor = get_role_oid(name2, true);
+ if (!OidIsValid(aip->ai_grantor))
+ ereturn(escontext, NULL,
+ (errcode(ERRCODE_UNDEFINED_OBJECT),
+ errmsg("role \"%s\" does not exist", name2)));
}
else
{
@@ -569,14 +592,19 @@ Datum
aclitemin(PG_FUNCTION_ARGS)
{
const char *s = PG_GETARG_CSTRING(0);
+ Node *escontext = fcinfo->context;
AclItem *aip;
aip = (AclItem *) palloc(sizeof(AclItem));
- s = aclparse(s, aip);
+
+ s = aclparse(s, aip, escontext);
+ if (s == NULL)
+ PG_RETURN_NULL();
+
while (isspace((unsigned char) *s))
++s;
if (*s)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("extra garbage at the end of the ACL specification")));
diff --git a/src/backend/utils/adt/int.c b/src/backend/utils/adt/int.c
index 8de38abd11d..2c90e526a60 100644
--- a/src/backend/utils/adt/int.c
+++ b/src/backend/utils/adt/int.c
@@ -141,6 +141,7 @@ Datum
int2vectorin(PG_FUNCTION_ARGS)
{
char *intString = PG_GETARG_CSTRING(0);
+ Node *escontext = fcinfo->context;
int2vector *result;
int n;
@@ -160,19 +161,19 @@ int2vectorin(PG_FUNCTION_ARGS)
l = strtol(intString, &endp, 10);
if (intString == endp)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"smallint", intString)));
if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s", intString,
"smallint")));
if (*endp && *endp != ' ')
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"integer", intString)));
@@ -183,7 +184,7 @@ int2vectorin(PG_FUNCTION_ARGS)
while (*intString && isspace((unsigned char) *intString))
intString++;
if (*intString)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("int2vector has too many elements")));
diff --git a/src/backend/utils/adt/oid.c b/src/backend/utils/adt/oid.c
index b5af4223412..9d382b5cb7c 100644
--- a/src/backend/utils/adt/oid.c
+++ b/src/backend/utils/adt/oid.c
@@ -19,6 +19,7 @@
#include "catalog/pg_type.h"
#include "libpq/pqformat.h"
+#include "nodes/miscnodes.h"
#include "nodes/value.h"
#include "utils/array.h"
#include "utils/builtins.h"
@@ -31,15 +32,26 @@
* USER I/O ROUTINES *
*****************************************************************************/
+/*
+ * Parse a single OID and return its value.
+ *
+ * If endloc isn't NULL, store a pointer to the rest of the string there,
+ * so that caller can parse the rest. Otherwise, it's an error if anything
+ * but whitespace follows.
+ *
+ * If escontext points to an ErrorSaveContext node, that is filled instead
+ * of throwing an error; the caller must check SOFT_ERROR_OCCURRED()
+ * to detect errors.
+ */
static Oid
-oidin_subr(const char *s, char **endloc)
+oidin_subr(const char *s, char **endloc, Node *escontext)
{
unsigned long cvt;
char *endptr;
Oid result;
if (*s == '\0')
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"oid", s)));
@@ -53,19 +65,19 @@ oidin_subr(const char *s, char **endloc)
* handled by the second "if" consistent across platforms.
*/
if (errno && errno != ERANGE && errno != EINVAL)
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"oid", s)));
if (endptr == s && *s != '\0')
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"oid", s)));
if (errno == ERANGE)
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s",
s, "oid")));
@@ -81,7 +93,7 @@ oidin_subr(const char *s, char **endloc)
while (*endptr && isspace((unsigned char) *endptr))
endptr++;
if (*endptr)
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"oid", s)));
@@ -104,7 +116,7 @@ oidin_subr(const char *s, char **endloc)
#if OID_MAX != ULONG_MAX
if (cvt != (unsigned long) result &&
cvt != (unsigned long) ((int) result))
- ereport(ERROR,
+ ereturn(escontext, InvalidOid,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type %s",
s, "oid")));
@@ -119,7 +131,7 @@ oidin(PG_FUNCTION_ARGS)
char *s = PG_GETARG_CSTRING(0);
Oid result;
- result = oidin_subr(s, NULL);
+ result = oidin_subr(s, NULL, fcinfo->context);
PG_RETURN_OID(result);
}
@@ -194,6 +206,7 @@ Datum
oidvectorin(PG_FUNCTION_ARGS)
{
char *oidString = PG_GETARG_CSTRING(0);
+ Node *escontext = fcinfo->context;
oidvector *result;
int n;
@@ -205,12 +218,14 @@ oidvectorin(PG_FUNCTION_ARGS)
oidString++;
if (*oidString == '\0')
break;
- result->values[n] = oidin_subr(oidString, &oidString);
+ result->values[n] = oidin_subr(oidString, &oidString, escontext);
+ if (SOFT_ERROR_OCCURRED(escontext))
+ PG_RETURN_NULL();
}
while (*oidString && isspace((unsigned char) *oidString))
oidString++;
if (*oidString)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("oidvector has too many elements")));
@@ -324,7 +339,7 @@ oidparse(Node *node)
* constants by the lexer. Accept these if they are valid OID
* strings.
*/
- return oidin_subr(castNode(Float, node)->fval, NULL);
+ return oidin_subr(castNode(Float, node)->fval, NULL, NULL);
default:
elog(ERROR, "unrecognized node type: %d", (int) nodeTag(node));
}
diff --git a/src/backend/utils/adt/pg_lsn.c b/src/backend/utils/adt/pg_lsn.c
index 15266f36f59..316a102ef30 100644
--- a/src/backend/utils/adt/pg_lsn.c
+++ b/src/backend/utils/adt/pg_lsn.c
@@ -69,7 +69,7 @@ pg_lsn_in(PG_FUNCTION_ARGS)
result = pg_lsn_in_internal(str, &have_error);
if (have_error)
- ereport(ERROR,
+ ereturn(fcinfo->context, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"pg_lsn", str)));
diff --git a/src/backend/utils/adt/tid.c b/src/backend/utils/adt/tid.c
index 83ac589f957..4dc1b327bf3 100644
--- a/src/backend/utils/adt/tid.c
+++ b/src/backend/utils/adt/tid.c
@@ -57,6 +57,7 @@ Datum
tidin(PG_FUNCTION_ARGS)
{
char *str = PG_GETARG_CSTRING(0);
+ Node *escontext = fcinfo->context;
char *p,
*coord[NTIDARGS];
int i;
@@ -71,7 +72,7 @@ tidin(PG_FUNCTION_ARGS)
coord[i++] = p + 1;
if (i < NTIDARGS)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"tid", str)));
@@ -79,7 +80,7 @@ tidin(PG_FUNCTION_ARGS)
errno = 0;
cvt = strtoul(coord[0], &badp, 10);
if (errno || *badp != DELIM)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"tid", str)));
@@ -93,7 +94,7 @@ tidin(PG_FUNCTION_ARGS)
#if SIZEOF_LONG > 4
if (cvt != (unsigned long) blockNumber &&
cvt != (unsigned long) ((int32) blockNumber))
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"tid", str)));
@@ -102,7 +103,7 @@ tidin(PG_FUNCTION_ARGS)
cvt = strtoul(coord[1], &badp, 10);
if (errno || *badp != RDELIM ||
cvt > USHRT_MAX)
- ereport(ERROR,
+ ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"tid", str)));
diff --git a/src/backend/utils/adt/xid8funcs.c b/src/backend/utils/adt/xid8funcs.c
index d8e40b3b969..2093776809f 100644
--- a/src/backend/utils/adt/xid8funcs.c
+++ b/src/backend/utils/adt/xid8funcs.c
@@ -285,7 +285,7 @@ buf_finalize(StringInfo buf)
* parse snapshot from cstring
*/
static pg_snapshot *
-parse_snapshot(const char *str)
+parse_snapshot(const char *str, Node *escontext)
{
FullTransactionId xmin;
FullTransactionId xmax;
@@ -341,11 +341,10 @@ parse_snapshot(const char *str)
return buf_finalize(buf);
bad_format:
- ereport(ERROR,
+ ereturn(escontext, NULL,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"",
"pg_snapshot", str_start)));
- return NULL; /* keep compiler quiet */
}
/*
@@ -447,7 +446,7 @@ pg_snapshot_in(PG_FUNCTION_ARGS)
char *str = PG_GETARG_CSTRING(0);
pg_snapshot *snap;
- snap = parse_snapshot(str);
+ snap = parse_snapshot(str, fcinfo->context);
PG_RETURN_POINTER(snap);
}