Fix overflow for INTERVAL 'x ms' where x is more than a couple million,
authorTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Aug 2009 21:23:14 +0000 (21:23 +0000)
committerTom Lane <tgl@sss.pgh.pa.us>
Tue, 18 Aug 2009 21:23:14 +0000 (21:23 +0000)
and integer datetimes are in use.  Per bug report from Hubert Depesz
Lubaczewski.

Alex Hunsaker

src/backend/utils/adt/datetime.c
src/include/utils/timestamp.h

index 78b1c2612b22dd78b370df495078a68819226e88..8c62b83c4db554c0294b0eefad87e14ca383c954 100644 (file)
@@ -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
index a40c4b646287c1607c07fb2464f4048a61c42181..45dd2c6f591f2e84ba9201ab9d04ccdfd534364f 100644 (file)
@@ -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