Detect Julian-date overflow in timestamp[tz]_pl_interval.
authorTom Lane <[email protected]>
Fri, 26 Jan 2024 18:39:37 +0000 (13:39 -0500)
committerTom Lane <[email protected]>
Fri, 26 Jan 2024 18:39:37 +0000 (13:39 -0500)
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

src/backend/utils/adt/timestamp.c
src/test/regress/expected/horology.out
src/test/regress/sql/horology.sql

index f6db57d2540bd7bb172d7e2c4d63f8af228621e9..b1fc30d00d4517999623e38ec3f24db6a8da1229 100644 (file)
@@ -2888,8 +2888,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, &timestamp) != 0)
@@ -2996,8 +3004,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);
index d56decd9940001f5c2be8a609a5d4f681537c64c..8ae0f8ce61d155937b4a4e3d86989fa4b779b3d7 100644 (file)
@@ -373,6 +373,8 @@ SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days'
  Sun Dec 31 00:00:00 294276
 (1 row)
 
+SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
+ERROR:  timestamp out of range
 SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
   106751991 Days  
 ------------------
@@ -633,6 +635,8 @@ SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS
  Fri Dec 31 23:59:59 1999 PST
 (1 row)
 
+SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
+ERROR:  timestamp out of range
 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
  True 
 ------
index fa92a80d0e661ee84a4d0fa96ea121193885f9e9..53fabed8af31a98b6e79ac74d6977e113545cdea 100644 (file)
@@ -86,6 +86,7 @@ SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second'
 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '106000000 days' AS "Feb 23, 285506";
 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '107000000 days' AS "Jan 20, 288244";
 SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days' AS "Dec 31, 294276";
+SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
 SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
 
 -- Shorthand values
@@ -117,6 +118,7 @@ SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
 SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
 SELECT timestamp with time zone '2000-03-01' - interval '1 second' AS "Feb 29";
 SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS "Dec 31";
+SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
 
 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
 SELECT (timestamp with time zone 'today' = (timestamp with time zone 'tomorrow' - interval '1 day')) as "True";