1
0
mirror of https://github.com/postgres/postgres.git synced 2025-11-04 20:11:56 +03:00

Convert a few more datatype input functions to report errors softly.

Convert bit_in, varbit_in, inet_in, cidr_in, macaddr_in, and
macaddr8_in to the new style.

Amul Sul, minor mods by me

Discussion: https://postgr.es/m/CAAJ_b97KeDWUdpTKGOaFYPv0OicjOu6EW+QYWj-Ywrgj_aEy1g@mail.gmail.com
This commit is contained in:
Tom Lane
2022-12-14 13:22:08 -05:00
parent b18c2decd7
commit 17407a8eaa
12 changed files with 230 additions and 56 deletions

View File

@@ -35,7 +35,7 @@
#define lobits(addr) \
((unsigned long)(((addr)->e<<24) | ((addr)->f<<16) | ((addr)->g<<8) | ((addr)->h)))
static unsigned char hex2_to_uchar(const unsigned char *ptr, const unsigned char *str);
static unsigned char hex2_to_uchar(const unsigned char *ptr, bool *badhex);
static const signed char hexlookup[128] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
@@ -51,16 +51,13 @@ static const signed char hexlookup[128] = {
/*
* hex2_to_uchar - convert 2 hex digits to a byte (unsigned char)
*
* This will ereport() if the end of the string is reached ('\0' found), or if
* Sets *badhex to true if the end of the string is reached ('\0' found), or if
* either character is not a valid hex digit.
*
* ptr is the pointer to where the digits to convert are in the string, str is
* the entire string, which is used only for error reporting.
*/
static inline unsigned char
hex2_to_uchar(const unsigned char *ptr, const unsigned char *str)
hex2_to_uchar(const unsigned char *ptr, bool *badhex)
{
unsigned char ret = 0;
unsigned char ret;
signed char lookup;
/* Handle the first character */
@@ -88,12 +85,7 @@ hex2_to_uchar(const unsigned char *ptr, const unsigned char *str)
return ret;
invalid_input:
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
/* We do not actually reach here */
*badhex = true;
return 0;
}
@@ -104,7 +96,9 @@ Datum
macaddr8_in(PG_FUNCTION_ARGS)
{
const unsigned char *str = (unsigned char *) PG_GETARG_CSTRING(0);
Node *escontext = fcinfo->context;
const unsigned char *ptr = str;
bool badhex = false;
macaddr8 *result;
unsigned char a = 0,
b = 0,
@@ -136,37 +130,37 @@ macaddr8_in(PG_FUNCTION_ARGS)
switch (count)
{
case 1:
a = hex2_to_uchar(ptr, str);
a = hex2_to_uchar(ptr, &badhex);
break;
case 2:
b = hex2_to_uchar(ptr, str);
b = hex2_to_uchar(ptr, &badhex);
break;
case 3:
c = hex2_to_uchar(ptr, str);
c = hex2_to_uchar(ptr, &badhex);
break;
case 4:
d = hex2_to_uchar(ptr, str);
d = hex2_to_uchar(ptr, &badhex);
break;
case 5:
e = hex2_to_uchar(ptr, str);
e = hex2_to_uchar(ptr, &badhex);
break;
case 6:
f = hex2_to_uchar(ptr, str);
f = hex2_to_uchar(ptr, &badhex);
break;
case 7:
g = hex2_to_uchar(ptr, str);
g = hex2_to_uchar(ptr, &badhex);
break;
case 8:
h = hex2_to_uchar(ptr, str);
h = hex2_to_uchar(ptr, &badhex);
break;
default:
/* must be trailing garbage... */
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
goto fail;
}
if (badhex)
goto fail;
/* Move forward to where the next byte should be */
ptr += 2;
@@ -179,10 +173,7 @@ macaddr8_in(PG_FUNCTION_ARGS)
/* Have to use the same spacer throughout */
else if (spacer != *ptr)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
goto fail;
/* move past the spacer */
ptr++;
@@ -197,10 +188,7 @@ macaddr8_in(PG_FUNCTION_ARGS)
/* If we found a space and then non-space, it's invalid */
if (*ptr)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
goto fail;
}
}
}
@@ -216,10 +204,7 @@ macaddr8_in(PG_FUNCTION_ARGS)
e = 0xFE;
}
else if (count != 8)
ereport(ERROR,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
goto fail;
result = (macaddr8 *) palloc0(sizeof(macaddr8));
@@ -233,6 +218,12 @@ macaddr8_in(PG_FUNCTION_ARGS)
result->h = h;
PG_RETURN_MACADDR8_P(result);
fail:
ereturn(escontext, (Datum) 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for type %s: \"%s\"", "macaddr8",
str)));
}
/*