Commit 3a4e929b authored by Tom Lane's avatar Tom Lane

Fix datetime input functions to correctly detect integer overflow when

running on a 64-bit platform ... strtol() will happily return 64-bit
output in that case.  Per bug #4231 from Geoff Tolley.
parent 5862cda6
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.189 2008/05/12 00:00:51 alvherre Exp $ * $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.190 2008/06/09 19:34:02 tgl Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
...@@ -255,6 +255,23 @@ static const datetkn *datecache[MAXDATEFIELDS] = {NULL}; ...@@ -255,6 +255,23 @@ static const datetkn *datecache[MAXDATEFIELDS] = {NULL};
static const datetkn *deltacache[MAXDATEFIELDS] = {NULL}; static const datetkn *deltacache[MAXDATEFIELDS] = {NULL};
/*
* strtoi --- just like strtol, but returns int not long
*/
static int
strtoi(const char *nptr, char **endptr, int base)
{
long val;
val = strtol(nptr, endptr, base);
#ifdef HAVE_LONG_INT_64
if (val != (long) ((int32) val))
errno = ERANGE;
#endif
return (int) val;
}
/* /*
* Calendar time to Julian date conversions. * Calendar time to Julian date conversions.
* Julian date is commonly used in astronomical applications, * Julian date is commonly used in astronomical applications,
...@@ -708,7 +725,7 @@ DecodeDateTime(char **field, int *ftype, int nf, ...@@ -708,7 +725,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
return DTERR_BAD_FORMAT; return DTERR_BAD_FORMAT;
errno = 0; errno = 0;
val = strtol(field[i], &cp, 10); val = strtoi(field[i], &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
...@@ -856,7 +873,7 @@ DecodeDateTime(char **field, int *ftype, int nf, ...@@ -856,7 +873,7 @@ DecodeDateTime(char **field, int *ftype, int nf,
int val; int val;
errno = 0; errno = 0;
val = strtol(field[i], &cp, 10); val = strtoi(field[i], &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
...@@ -1591,7 +1608,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf, ...@@ -1591,7 +1608,7 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
} }
errno = 0; errno = 0;
val = strtol(field[i], &cp, 10); val = strtoi(field[i], &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
...@@ -2214,14 +2231,14 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec) ...@@ -2214,14 +2231,14 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec)
*tmask = DTK_TIME_M; *tmask = DTK_TIME_M;
errno = 0; errno = 0;
tm->tm_hour = strtol(str, &cp, 10); tm->tm_hour = strtoi(str, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
if (*cp != ':') if (*cp != ':')
return DTERR_BAD_FORMAT; return DTERR_BAD_FORMAT;
str = cp + 1; str = cp + 1;
errno = 0; errno = 0;
tm->tm_min = strtol(str, &cp, 10); tm->tm_min = strtoi(str, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
if (*cp == '\0') if (*cp == '\0')
...@@ -2235,7 +2252,7 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec) ...@@ -2235,7 +2252,7 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec)
{ {
str = cp + 1; str = cp + 1;
errno = 0; errno = 0;
tm->tm_sec = strtol(str, &cp, 10); tm->tm_sec = strtoi(str, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
if (*cp == '\0') if (*cp == '\0')
...@@ -2289,7 +2306,7 @@ DecodeNumber(int flen, char *str, bool haveTextMonth, int fmask, ...@@ -2289,7 +2306,7 @@ DecodeNumber(int flen, char *str, bool haveTextMonth, int fmask,
*tmask = 0; *tmask = 0;
errno = 0; errno = 0;
val = strtol(str, &cp, 10); val = strtoi(str, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
if (cp == str) if (cp == str)
...@@ -2580,7 +2597,7 @@ DecodeTimezone(char *str, int *tzp) ...@@ -2580,7 +2597,7 @@ DecodeTimezone(char *str, int *tzp)
return DTERR_BAD_FORMAT; return DTERR_BAD_FORMAT;
errno = 0; errno = 0;
hr = strtol(str + 1, &cp, 10); hr = strtoi(str + 1, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_TZDISP_OVERFLOW; return DTERR_TZDISP_OVERFLOW;
...@@ -2588,13 +2605,13 @@ DecodeTimezone(char *str, int *tzp) ...@@ -2588,13 +2605,13 @@ DecodeTimezone(char *str, int *tzp)
if (*cp == ':') if (*cp == ':')
{ {
errno = 0; errno = 0;
min = strtol(cp + 1, &cp, 10); min = strtoi(cp + 1, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_TZDISP_OVERFLOW; return DTERR_TZDISP_OVERFLOW;
if (*cp == ':') if (*cp == ':')
{ {
errno = 0; errno = 0;
sec = strtol(cp + 1, &cp, 10); sec = strtoi(cp + 1, &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_TZDISP_OVERFLOW; return DTERR_TZDISP_OVERFLOW;
} }
...@@ -2784,7 +2801,7 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm, ...@@ -2784,7 +2801,7 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
case DTK_DATE: case DTK_DATE:
case DTK_NUMBER: case DTK_NUMBER:
errno = 0; errno = 0;
val = strtol(field[i], &cp, 10); val = strtoi(field[i], &cp, 10);
if (errno == ERANGE) if (errno == ERANGE)
return DTERR_FIELD_OVERFLOW; return DTERR_FIELD_OVERFLOW;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment