Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit c3bdb25

Browse files
committed
Detect Julian-date overflow in timestamp[tz]_pl_interval.
We perform addition of the days field of an interval via arithmetic on the Julian-date representation of the timestamp's date. This step is subject to int32 overflow, and we also should not let the Julian date become very negative, for fear of weird results from j2date. (In the timestamptz case, allow a Julian date of -1 to pass, since it might convert back to zero after timezone rotation.) The additions of the months and microseconds fields could also overflow, of course. However, I believe we need no additional checks there; the existing range checks should catch such cases. The difficulty here is that j2date's magic modular arithmetic could produce something that looks like it's in-range. Per bug #18313 from Christian Maurer. This has been wrong for a long time, so back-patch to all supported branches. Discussion: https://postgr.es/m/18313-64d2c8952d81e84b@postgresql.org
1 parent d060cb6 commit c3bdb25

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2893,8 +2893,16 @@ timestamp_pl_interval(PG_FUNCTION_ARGS)
28932893
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
28942894
errmsg("timestamp out of range")));
28952895

2896-
/* Add days by converting to and from Julian */
2897-
julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day;
2896+
/*
2897+
* Add days by converting to and from Julian. We need an overflow
2898+
* check here since j2date expects a non-negative integer input.
2899+
*/
2900+
julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
2901+
if (pg_add_s32_overflow(julian, span->day, &julian) ||
2902+
julian < 0)
2903+
ereport(ERROR,
2904+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2905+
errmsg("timestamp out of range")));
28982906
j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
28992907

29002908
if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0)
@@ -3001,8 +3009,19 @@ timestamptz_pl_interval(PG_FUNCTION_ARGS)
30013009
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
30023010
errmsg("timestamp out of range")));
30033011

3004-
/* Add days by converting to and from Julian */
3005-
julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) + span->day;
3012+
/*
3013+
* Add days by converting to and from Julian. We need an overflow
3014+
* check here since j2date expects a non-negative integer input.
3015+
* In practice though, it will give correct answers for small
3016+
* negative Julian dates; we should allow -1 to avoid
3017+
* timezone-dependent failures, as discussed in timestamp.h.
3018+
*/
3019+
julian = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday);
3020+
if (pg_add_s32_overflow(julian, span->day, &julian) ||
3021+
julian < -1)
3022+
ereport(ERROR,
3023+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3024+
errmsg("timestamp out of range")));
30063025
j2date(julian, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
30073026

30083027
tz = DetermineTimeZoneOffset(tm, session_timezone);

src/test/regress/expected/horology.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,8 @@ SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days'
367367
Sun Dec 31 00:00:00 294276
368368
(1 row)
369369

370+
SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
371+
ERROR: timestamp out of range
370372
SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
371373
106751991 Days
372374
------------------
@@ -627,6 +629,8 @@ SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS
627629
Fri Dec 31 23:59:59 1999 PST
628630
(1 row)
629631

632+
SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
633+
ERROR: timestamp out of range
630634
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
631635
True
632636
------

src/test/regress/sql/horology.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second'
8484
SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '106000000 days' AS "Feb 23, 285506";
8585
SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '107000000 days' AS "Jan 20, 288244";
8686
SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days' AS "Dec 31, 294276";
87+
SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
8788
SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
8889

8990
-- Shorthand values
@@ -115,6 +116,7 @@ SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
115116
SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
116117
SELECT timestamp with time zone '2000-03-01' - interval '1 second' AS "Feb 29";
117118
SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
119+
SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
118120

119121
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
120122
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";

0 commit comments

Comments
 (0)