Skip to content

Commit 841b4a2

Browse files
committed
tm2timestamp should return -1, not elog, on overflow. (In the backend
this is merely an API inconsistency, but in ecpg it's fatal.) Also, fix misconceived overflow test in HAVE_INT64_TIMESTAMP case.
1 parent 3abbce3 commit 841b4a2

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/backend/utils/adt/timestamp.c

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.84 2003/05/12 23:08:50 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/utils/adt/timestamp.c,v 1.85 2003/07/04 18:21:13 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1052,6 +1052,8 @@ timestamp2tm(Timestamp dt, int *tzp, struct tm * tm, fsec_t *fsec, char **tzn)
10521052
* Convert a tm structure to a timestamp data type.
10531053
* Note that year is _not_ 1900-based, but is an explicit full value.
10541054
* Also, month is one-based, _not_ zero-based.
1055+
*
1056+
* Returns -1 on failure (overflow).
10551057
*/
10561058
int
10571059
tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
@@ -1072,10 +1074,13 @@ tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
10721074
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - POSTGRES_EPOCH_JDATE;
10731075
time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
10741076
#ifdef HAVE_INT64_TIMESTAMP
1075-
*result = ((date * INT64CONST(86400000000)) + time);
1076-
if ((*result < 0 && date >= 0) || (*result >= 0 && date < 0))
1077-
elog(ERROR, "TIMESTAMP out of range '%04d-%02d-%02d'",
1078-
tm->tm_year, tm->tm_mon, tm->tm_mday);
1077+
*result = (date * INT64CONST(86400000000)) + time;
1078+
/* check for major overflow */
1079+
if ((*result - time) / INT64CONST(86400000000) != date)
1080+
return -1;
1081+
/* check for just-barely overflow (okay except time-of-day wraps) */
1082+
if ((*result < 0) ? (date >= 0) : (date < 0))
1083+
return -1;
10791084
#else
10801085
*result = ((date * 86400) + time);
10811086
#endif

src/interfaces/ecpg/pgtypeslib/timestamp.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ dt2local(Timestamp dt, int tz)
4444
* Convert a tm structure to a timestamp data type.
4545
* Note that year is _not_ 1900-based, but is an explicit full value.
4646
* Also, month is one-based, _not_ zero-based.
47+
*
48+
* Returns -1 on failure (overflow).
4749
*/
4850
static int
4951
tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
@@ -64,10 +66,13 @@ tm2timestamp(struct tm * tm, fsec_t fsec, int *tzp, Timestamp *result)
6466
date = date2j(tm->tm_year, tm->tm_mon, tm->tm_mday) - date2j(2000, 1, 1);
6567
time = time2t(tm->tm_hour, tm->tm_min, tm->tm_sec, fsec);
6668
#ifdef HAVE_INT64_TIMESTAMP
67-
*result = ((date * INT64CONST(86400000000)) + time);
68-
if ((*result < 0 && date >= 0) || (*result >= 0 && date < 0))
69-
elog(ERROR, "TIMESTAMP out of range '%04d-%02d-%02d'",
70-
tm->tm_year, tm->tm_mon, tm->tm_mday);
69+
*result = (date * INT64CONST(86400000000)) + time;
70+
/* check for major overflow */
71+
if ((*result - time) / INT64CONST(86400000000) != date)
72+
return -1;
73+
/* check for just-barely overflow (okay except time-of-day wraps) */
74+
if ((*result < 0) ? (date >= 0) : (date < 0))
75+
return -1;
7176
#else
7277
*result = ((date * 86400) + time);
7378
#endif

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy