Skip to content

Commit

Permalink
Adapt some casts for "soft" error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
robozmey committed Dec 11, 2024
1 parent 5091b5a commit 7f642b8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/backend/utils/adt/int.c
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ int4in(PG_FUNCTION_ARGS)
{
char *num = PG_GETARG_CSTRING(0);

PG_RETURN_INT32(pg_atoi(num, sizeof(int32), '\0'));
PG_RETURN_INT32(pg_atoi_safe(num, sizeof(int32), '\0', fcinfo->context));
}

/*
Expand Down Expand Up @@ -344,7 +344,7 @@ i4toi2(PG_FUNCTION_ARGS)
int32 arg1 = PG_GETARG_INT32(0);

if (arg1 < SHRT_MIN || arg1 > SHRT_MAX)
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("smallint out of range")));

Expand Down
18 changes: 12 additions & 6 deletions src/backend/utils/adt/numutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,12 @@
*/
int32
pg_atoi(char *s, int size, int c)
{
return pg_atoi_safe(s, size, c, NULL);
}

int32
pg_atoi_safe(char *s, int size, int c, Node *escontext)
{
long l;
char *badp;
Expand All @@ -46,7 +52,7 @@ pg_atoi(char *s, int size, int c)
if (s == NULL)
elog(ERROR, "NULL pointer");
if (*s == 0)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for integer: \"%s\"",
s)));
Expand All @@ -56,7 +62,7 @@ pg_atoi(char *s, int size, int c)

/* We made no progress parsing the string, so bail out */
if (s == badp)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for integer: \"%s\"",
s)));
Expand All @@ -70,19 +76,19 @@ pg_atoi(char *s, int size, int c)
|| l < INT_MIN || l > INT_MAX
#endif
)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type integer", s)));
break;
case sizeof(int16):
if (errno == ERANGE || l < SHRT_MIN || l > SHRT_MAX)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for type smallint", s)));
break;
case sizeof(int8):
if (errno == ERANGE || l < SCHAR_MIN || l > SCHAR_MAX)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_NUMERIC_VALUE_OUT_OF_RANGE),
errmsg("value \"%s\" is out of range for 8-bit integer", s)));
break;
Expand All @@ -98,7 +104,7 @@ pg_atoi(char *s, int size, int c)
badp++;

if (*badp && *badp != c)
ereport(ERROR,
ereturn(escontext, 0,
(errcode(ERRCODE_INVALID_TEXT_REPRESENTATION),
errmsg("invalid input syntax for integer: \"%s\"",
s)));
Expand Down

0 comments on commit 7f642b8

Please sign in to comment.