Fix overflow check in tm2timestamp (this time for sure).
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 4 Mar 2013 20:13:31 +0000 (15:13 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 4 Mar 2013 20:14:13 +0000 (15:14 -0500)
I fixed this code back in commit 841b4a2d5, but didn't think carefully
enough about the behavior near zero, which meant it improperly rejected
1999-12-31 24:00:00.  Per report from Magnus Hagander.

src/backend/utils/adt/timestamp.c
src/interfaces/ecpg/pgtypeslib/timestamp.c

index a0c5a6ab66a617cd756e22837527f8454077d11d..88c3f7d262b29a91099a1c775f6050473df1418e 100644 (file)
@@ -1564,8 +1564,9 @@ tm2timestamp(struct pg_tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
                return -1;
        }
        /* check for just-barely overflow (okay except time-of-day wraps) */
-       if ((*result < 0 && date >= 0) ||
-               (*result >= 0 && date < 0))
+       /* caution: we want to allow 1999-12-31 24:00:00 */
+       if ((*result < 0 && date > 0) ||
+               (*result > 0 && date < -1))
        {
                *result = 0;                    /* keep compiler quiet */
                return -1;
index 315c683455a17fb023700cff867084fc7bca9a0f..46084fe87692e1e21b7e8a6703b89a4725093901 100644 (file)
@@ -76,8 +76,9 @@ tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, timestamp * result)
        if ((*result - time) / USECS_PER_DAY != dDate)
                return -1;
        /* check for just-barely overflow (okay except time-of-day wraps) */
-       if ((*result < 0 && dDate >= 0) ||
-               (*result >= 0 && dDate < 0))
+       /* caution: we want to allow 1999-12-31 24:00:00 */
+       if ((*result < 0 && dDate > 0) ||
+               (*result > 0 && dDate < -1))
                return -1;
 #else
        *result = dDate * SECS_PER_DAY + time;