Skip to content

Commit

Permalink
Convert nabstime.c functions for soft errors
Browse files Browse the repository at this point in the history
  • Loading branch information
robozmey committed Dec 16, 2024
1 parent d168f8c commit 50cf8d0
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 38 deletions.
39 changes: 23 additions & 16 deletions src/backend/utils/adt/date.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ static void AdjustTimeForTypmod(TimeADT *time, int32 typmod);


/* common code for timetypmodin and timetztypmodin */
// static int32
// anytime_typmodin(bool istz, ArrayType *ta)
// {

// }

static bool
anytime_typmodin_safe(bool istz, ArrayType *ta, int32 *result, Node *escontext)
{
Expand Down Expand Up @@ -93,6 +87,14 @@ anytime_typmodin_safe(bool istz, ArrayType *ta, int32 *result, Node *escontext)
return true;
}

static int32
anytime_typmodin(bool istz, ArrayType *ta)
{
int32 result;
(void) anytime_typmodin_safe(istz, ta, &result, NULL);
return result;
}

/* common code for timetypmodout and timetztypmodout */
static char *
anytime_typmodout(bool istz, int32 typmod)
Expand Down Expand Up @@ -462,12 +464,6 @@ date_mii(PG_FUNCTION_ARGS)
* Internal routines for promoting date to timestamp and timestamp with
* time zone
*/
// static Timestamp
// date2timestamp(DateADT dateVal)
// {

// }

static bool
date2timestamp_safe(DateADT dateVal, Timestamp *res, Node *escontext)
{
Expand Down Expand Up @@ -497,10 +493,13 @@ date2timestamp_safe(DateADT dateVal, Timestamp *res, Node *escontext)
return true;
}

// static TimestampTz
// date2timestamptz(DateADT dateVal)
// {
// }
static Timestamp
date2timestamp(DateADT dateVal)
{
Timestamp result;
(void) date2timestamp_safe(dateVal, &result, NULL);
return result;
}

static bool
date2timestamptz_safe(DateADT dateVal, TimestampTz *res, Node *escontext)
Expand Down Expand Up @@ -539,6 +538,14 @@ date2timestamptz_safe(DateADT dateVal, TimestampTz *res, Node *escontext)
return true;
}

static TimestampTz
date2timestamptz(DateADT dateVal)
{
TimestampTz result;
(void) date2timestamptz_safe(dateVal, &result, NULL);
return result;
}

/*
* date2timestamp_no_overflow
*
Expand Down
24 changes: 13 additions & 11 deletions src/backend/utils/adt/datetime.c
Original file line number Diff line number Diff line change
Expand Up @@ -3278,7 +3278,7 @@ int
DecodeTimezoneAbbrev(int field, char *lowtoken,
int *offset, pg_tz **tz)
{
int result;
int result = 0;
(void) DecodeTimezoneAbbrevSafe(field, lowtoken, offset, tz, &result, NULL);
return result;
}
Expand Down Expand Up @@ -4084,11 +4084,11 @@ DecodeUnits(int field, char *lowtoken, int *val)
* DTERR_TZDISP_OVERFLOW from DTERR_FIELD_OVERFLOW, but SQL99 mandates three
* separate SQLSTATE codes, so ...
*/
// void
// DateTimeParseError(int dterr, const char *str, const char *datatype)
// {
// (void) DateTimeParseErrorSafe(dterr, str, datatype, NULL);
// }
void
DateTimeParseError(int dterr, const char *str, const char *datatype)
{
(void) DateTimeParseErrorSafe(dterr, str, datatype, NULL);
}

bool
DateTimeParseErrorSafe(int dterr, const char *str, const char *datatype, Node *escontext)
Expand Down Expand Up @@ -4938,11 +4938,13 @@ InstallTimeZoneAbbrevs(TimeZoneAbbrevTable *tbl)
/*
* Helper subroutine to locate pg_tz timezone for a dynamic abbreviation.
*/
// static pg_tz *
// FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp)
// {
// FetchDynamicTimeZoneSafe
// }
static pg_tz *
FetchDynamicTimeZone(TimeZoneAbbrevTable *tbl, const datetkn *tp)
{
pg_tz *result;
FetchDynamicTimeZoneSafe(tbl, tp, &result, NULL);
return result;
}

static bool
FetchDynamicTimeZoneSafe(TimeZoneAbbrevTable *tbl, const datetkn *tp, pg_tz **result, Node *escontext)
Expand Down
36 changes: 25 additions & 11 deletions src/backend/utils/adt/nabstime.c
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ static void reltime2tm(RelativeTime time, struct pg_tm * tm);
static void parsetinterval(char *i_string,
AbsoluteTime *i_start,
AbsoluteTime *i_end);
static bool parsetinterval_safe(char *i_string,
AbsoluteTime *i_start,
AbsoluteTime *i_end,
Node *escontext);


/*
Expand Down Expand Up @@ -455,7 +459,7 @@ timestamp_abstime(PG_FUNCTION_ARGS)
}
else
{
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
result = INVALID_ABSTIME;
Expand All @@ -481,7 +485,7 @@ abstime_timestamp(PG_FUNCTION_ARGS)
switch (abstime)
{
case INVALID_ABSTIME:
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot convert abstime \"invalid\" to timestamp")));
TIMESTAMP_NOBEGIN(result);
Expand All @@ -498,7 +502,7 @@ abstime_timestamp(PG_FUNCTION_ARGS)
default:
abstime2tm(abstime, &tz, tm, &tzn);
if (tm2timestamp(tm, 0, NULL, &result) != 0)
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
Expand Down Expand Up @@ -528,7 +532,7 @@ timestamptz_abstime(PG_FUNCTION_ARGS)
result = tm2abstime(tm, 0);
else
{
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
result = INVALID_ABSTIME;
Expand All @@ -554,7 +558,7 @@ abstime_timestamptz(PG_FUNCTION_ARGS)
switch (abstime)
{
case INVALID_ABSTIME:
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot convert abstime \"invalid\" to timestamp")));
TIMESTAMP_NOBEGIN(result);
Expand All @@ -571,7 +575,7 @@ abstime_timestamptz(PG_FUNCTION_ARGS)
default:
abstime2tm(abstime, &tz, tm, &tzn);
if (tm2timestamp(tm, 0, &tz, &result) != 0)
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
errmsg("timestamp out of range")));
break;
Expand Down Expand Up @@ -711,7 +715,8 @@ tintervalin(PG_FUNCTION_ARGS)
t1,
t2;

parsetinterval(tintervalstr, &t1, &t2);
if (!parsetinterval_safe(tintervalstr, &t1, &t2, fcinfo->context))
PG_RETURN_NULL();

tinterval = (TimeInterval) palloc(sizeof(TimeIntervalData));

Expand Down Expand Up @@ -782,7 +787,7 @@ tintervalrecv(PG_FUNCTION_ARGS)
status = T_INTERVAL_VALID;

if (status != tinterval->status)
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_INVALID_BINARY_REPRESENTATION),
errmsg("invalid status in external \"tinterval\" value")));

Expand Down Expand Up @@ -856,7 +861,7 @@ reltime_interval(PG_FUNCTION_ARGS)
switch (reltime)
{
case INVALID_RELTIME:
ereport(ERROR,
ereturn(fcinfo->context, 0,
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
errmsg("cannot convert reltime \"invalid\" to interval")));
result->time = 0;
Expand Down Expand Up @@ -1449,6 +1454,14 @@ static void
parsetinterval(char *i_string,
AbsoluteTime *i_start,
AbsoluteTime *i_end)
{
(void) parsetinterval_safe(i_string, i_start, i_end, NULL);
}
static bool
parsetinterval_safe(char *i_string,
AbsoluteTime *i_start,
AbsoluteTime *i_end,
Node *escontext)
{
char *p,
*p1;
Expand Down Expand Up @@ -1548,14 +1561,15 @@ parsetinterval(char *i_string,
goto bogus; /* syntax error */

/* it seems to be a valid tinterval */
return;
return true;

bogus:
ereport(ERROR,
ereturn(escontext, false,
(errcode(ERRCODE_INVALID_DATETIME_FORMAT),
errmsg("invalid input syntax for type tinterval: \"%s\"",
i_string)));
*i_start = *i_end = INVALID_ABSTIME; /* keep compiler quiet */
return false;
}


Expand Down

0 comments on commit 50cf8d0

Please sign in to comment.