1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-08 11:42:09 +03:00

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
This commit is contained in:
Tom Lane
2022-12-14 19:42:05 -05:00
parent 90161dad4d
commit 3b9d2deb67
14 changed files with 203 additions and 30 deletions

View File

@ -171,8 +171,8 @@ hex_encode(const char *src, size_t len, char *dst)
return (uint64) len * 2;
}
static inline char
get_hex(const char *cp)
static inline bool
get_hex(const char *cp, char *out)
{
unsigned char c = (unsigned char) *cp;
int res = -1;
@ -180,17 +180,19 @@ get_hex(const char *cp)
if (c < 127)
res = hexlookup[c];
if (res < 0)
ereport(ERROR,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid hexadecimal digit: \"%.*s\"",
pg_mblen(cp), cp)));
*out = (char) res;
return (char) res;
return (res >= 0);
}
uint64
hex_decode(const char *src, size_t len, char *dst)
{
return hex_decode_safe(src, len, dst, NULL);
}
uint64
hex_decode_safe(const char *src, size_t len, char *dst, Node *escontext)
{
const char *s,
*srcend;
@ -208,16 +210,23 @@ hex_decode(const char *src, size_t len, char *dst)
s++;
continue;
}
v1 = get_hex(s) << 4;
if (!get_hex(s, &v1))
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid hexadecimal digit: \"%.*s\"",
pg_mblen(s), s)));
s++;
if (s >= srcend)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid hexadecimal data: odd number of digits")));
v2 = get_hex(s);
if (!get_hex(s, &v2))
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_PARAMETER_VALUE),
errmsg("invalid hexadecimal digit: \"%.*s\"",
pg_mblen(s), s)));
s++;
*p++ = v1 | v2;
*p++ = (v1 << 4) | v2;
}
return p - dst;