diff options
Diffstat (limited to 'src/backend/utils/adt/timestamp.c')
-rw-r--r-- | src/backend/utils/adt/timestamp.c | 27 |
1 files changed, 23 insertions, 4 deletions
diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index fbc6bad20d6..b41a08878a0 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -2893,8 +2893,16 @@ timestamp_pl_interval(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - /* Add days by converting to and from Julian */ - julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day; + /* + * Add days by converting to and from Julian. We need an overflow + * check here since j2date expects a non-negative integer input. + */ + julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday); + if (pg_add_s32_overflow(julian, span->day, &julian) || + julian < 0) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range"))); j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); if (tm2timestamp(tm, fsec, NULL, ×tamp) != 0) @@ -3001,8 +3009,19 @@ timestamptz_pl_interval(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("timestamp out of range"))); - /* Add days by converting to and from Julian */ - julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day; + /* + * Add days by converting to and from Julian. We need an overflow + * check here since j2date expects a non-negative integer input. + * In practice though, it will give correct answers for small + * negative Julian dates; we should allow -1 to avoid + * timezone-dependent failures, as discussed in timestamp.h. + */ + julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday); + if (pg_add_s32_overflow(julian, span->day, &julian) || + julian < -1) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("timestamp out of range"))); j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday); tz = DetermineTimeZoneOffset(tm, session_timezone); |