diff options
author | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-14 19:42:05 -0500 |
---|---|---|
committer | Tom Lane <tgl@sss.pgh.pa.us> | 2022-12-14 19:42:05 -0500 |
commit | 3b9d2deb67a0d5ee681561fc0e07c2dca4e26e96 (patch) | |
tree | 071bf2c50f40fecd2a15e84a402b7a7749564d9a /src/backend/utils/adt/varchar.c | |
parent | 90161dad4dd719f44255474f4471af916f2a28f0 (diff) | |
download | postgresql-3b9d2deb67a0d5ee681561fc0e07c2dca4e26e96.tar.gz postgresql-3b9d2deb67a0d5ee681561fc0e07c2dca4e26e96.zip |
Convert a few more datatype input functions to report errors softly.
Convert the remaining string-category input functions
(bpcharin, varcharin, byteain) to the new style.
Discussion: https://postgr.es/m/3038346.1671060258@sss.pgh.pa.us
Diffstat (limited to 'src/backend/utils/adt/varchar.c')
-rw-r--r-- | src/backend/utils/adt/varchar.c | 33 |
1 files changed, 19 insertions, 14 deletions
diff --git a/src/backend/utils/adt/varchar.c b/src/backend/utils/adt/varchar.c index a63c498181e..01a2db6b23b 100644 --- a/src/backend/utils/adt/varchar.c +++ b/src/backend/utils/adt/varchar.c @@ -122,9 +122,13 @@ anychar_typmodout(int32 typmod) * * If the input string is too long, raise an error, unless the extra * characters are spaces, in which case they're truncated. (per SQL) + * + * 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 BpChar * -bpchar_input(const char *s, size_t len, int32 atttypmod) +bpchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext) { BpChar *result; char *r; @@ -153,7 +157,7 @@ bpchar_input(const char *s, size_t len, int32 atttypmod) for (j = mbmaxlen; j < len; j++) { if (s[j] != ' ') - ereport(ERROR, + ereturn(escontext, NULL, (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), errmsg("value too long for type character(%d)", (int) maxlen))); @@ -195,14 +199,13 @@ Datum bpcharin(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); - #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif int32 atttypmod = PG_GETARG_INT32(2); BpChar *result; - result = bpchar_input(s, strlen(s), atttypmod); + result = bpchar_input(s, strlen(s), atttypmod, fcinfo->context); PG_RETURN_BPCHAR_P(result); } @@ -228,7 +231,6 @@ Datum bpcharrecv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); - #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif @@ -238,7 +240,7 @@ bpcharrecv(PG_FUNCTION_ARGS) int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); - result = bpchar_input(str, nbytes, atttypmod); + result = bpchar_input(str, nbytes, atttypmod, NULL); pfree(str); PG_RETURN_BPCHAR_P(result); } @@ -448,11 +450,12 @@ bpchartypmodout(PG_FUNCTION_ARGS) * If the input string is too long, raise an error, unless the extra * characters are spaces, in which case they're truncated. (per SQL) * - * Uses the C string to text conversion function, which is only appropriate - * if VarChar and text are equivalent types. + * 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 VarChar * -varchar_input(const char *s, size_t len, int32 atttypmod) +varchar_input(const char *s, size_t len, int32 atttypmod, Node *escontext) { VarChar *result; size_t maxlen; @@ -468,7 +471,7 @@ varchar_input(const char *s, size_t len, int32 atttypmod) for (j = mbmaxlen; j < len; j++) { if (s[j] != ' ') - ereport(ERROR, + ereturn(escontext, NULL, (errcode(ERRCODE_STRING_DATA_RIGHT_TRUNCATION), errmsg("value too long for type character varying(%d)", (int) maxlen))); @@ -477,6 +480,10 @@ varchar_input(const char *s, size_t len, int32 atttypmod) len = mbmaxlen; } + /* + * We can use cstring_to_text_with_len because VarChar and text are + * binary-compatible types. + */ result = (VarChar *) cstring_to_text_with_len(s, len); return result; } @@ -489,14 +496,13 @@ Datum varcharin(PG_FUNCTION_ARGS) { char *s = PG_GETARG_CSTRING(0); - #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif int32 atttypmod = PG_GETARG_INT32(2); VarChar *result; - result = varchar_input(s, strlen(s), atttypmod); + result = varchar_input(s, strlen(s), atttypmod, fcinfo->context); PG_RETURN_VARCHAR_P(result); } @@ -522,7 +528,6 @@ Datum varcharrecv(PG_FUNCTION_ARGS) { StringInfo buf = (StringInfo) PG_GETARG_POINTER(0); - #ifdef NOT_USED Oid typelem = PG_GETARG_OID(1); #endif @@ -532,7 +537,7 @@ varcharrecv(PG_FUNCTION_ARGS) int nbytes; str = pq_getmsgtext(buf, buf->len - buf->cursor, &nbytes); - result = varchar_input(str, nbytes, atttypmod); + result = varchar_input(str, nbytes, atttypmod, NULL); pfree(str); PG_RETURN_VARCHAR_P(result); } |