From fa8caa0c24826c5db453003833c26f3d593a80ea Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Tue, 18 Aug 2009 21:23:14 +0000 Subject: [PATCH] Fix overflow for INTERVAL 'x ms' where x is more than a couple million, and integer datetimes are in use. Per bug report from Hubert Depesz Lubaczewski. Alex Hunsaker --- src/backend/utils/adt/datetime.c | 3 +++ src/include/utils/timestamp.h | 2 ++ 2 files changed, 5 insertions(+) diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c index 78b1c2612b..8c62b83c4d 100644 --- a/src/backend/utils/adt/datetime.c +++ b/src/backend/utils/adt/datetime.c @@ -2986,6 +2986,9 @@ DecodeInterval(char **field, int *ftype, int nf, int range, break; case DTK_MILLISEC: + /* avoid overflowing the fsec field */ + tm->tm_sec += val / 1000; + val -= (val / 1000) * 1000; #ifdef HAVE_INT64_TIMESTAMP *fsec += rint((val + fval) * 1000); #else diff --git a/src/include/utils/timestamp.h b/src/include/utils/timestamp.h index a40c4b6462..45dd2c6f59 100644 --- a/src/include/utils/timestamp.h +++ b/src/include/utils/timestamp.h @@ -39,6 +39,8 @@ * TimeOffset and fsec_t are convenience typedefs for temporary variables * that are of different types in the two cases. Do not use fsec_t in values * stored on-disk, since it is not the same size in both implementations. + * Also, fsec_t is only meant for *fractional* seconds; beware of overflow + * if the value you need to store could be many seconds. */ #ifdef HAVE_INT64_TIMESTAMP -- 2.30.2