Skip to content

Commit 814acfc

Browse files
committed
Check for overflow in strtol() while parsing datetime inputs.
Michael Fuhr.
1 parent cab4081 commit 814acfc

File tree

1 file changed

+33
-1
lines changed

1 file changed

+33
-1
lines changed

src/backend/utils/adt/datetime.c

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.161 2005/11/22 18:17:22 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/datetime.c,v 1.162 2005/12/01 17:56:34 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -1013,7 +1013,10 @@ DecodeDateTime(char **field, int *ftype, int nf,
10131013
if (tzp == NULL)
10141014
return DTERR_BAD_FORMAT;
10151015

1016+
errno = 0;
10161017
val = strtol(field[i], &cp, 10);
1018+
if (errno == ERANGE)
1019+
return DTERR_FIELD_OVERFLOW;
10171020

10181021
j2date(val, &tm->tm_year, &tm->tm_mon, &tm->tm_mday);
10191022
/* Get the time zone from the end of the string */
@@ -1158,7 +1161,10 @@ DecodeDateTime(char **field, int *ftype, int nf,
11581161
char *cp;
11591162
int val;
11601163

1164+
errno = 0;
11611165
val = strtol(field[i], &cp, 10);
1166+
if (errno == ERANGE)
1167+
return DTERR_FIELD_OVERFLOW;
11621168

11631169
/*
11641170
* only a few kinds are allowed to have an embedded
@@ -1915,7 +1921,10 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
19151921
break;
19161922
}
19171923

1924+
errno = 0;
19181925
val = strtol(field[i], &cp, 10);
1926+
if (errno == ERANGE)
1927+
return DTERR_FIELD_OVERFLOW;
19191928

19201929
/*
19211930
* only a few kinds are allowed to have an embedded
@@ -2456,11 +2465,17 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec)
24562465

24572466
*tmask = DTK_TIME_M;
24582467

2468+
errno = 0;
24592469
tm->tm_hour = strtol(str, &cp, 10);
2470+
if (errno == ERANGE)
2471+
return DTERR_FIELD_OVERFLOW;
24602472
if (*cp != ':')
24612473
return DTERR_BAD_FORMAT;
24622474
str = cp + 1;
2475+
errno = 0;
24632476
tm->tm_min = strtol(str, &cp, 10);
2477+
if (errno == ERANGE)
2478+
return DTERR_FIELD_OVERFLOW;
24642479
if (*cp == '\0')
24652480
{
24662481
tm->tm_sec = 0;
@@ -2471,7 +2486,10 @@ DecodeTime(char *str, int fmask, int *tmask, struct pg_tm * tm, fsec_t *fsec)
24712486
else
24722487
{
24732488
str = cp + 1;
2489+
errno = 0;
24742490
tm->tm_sec = strtol(str, &cp, 10);
2491+
if (errno == ERANGE)
2492+
return DTERR_FIELD_OVERFLOW;
24752493
if (*cp == '\0')
24762494
*fsec = 0;
24772495
else if (*cp == '.')
@@ -2522,7 +2540,10 @@ DecodeNumber(int flen, char *str, bool haveTextMonth, int fmask,
25222540

25232541
*tmask = 0;
25242542

2543+
errno = 0;
25252544
val = strtol(str, &cp, 10);
2545+
if (errno == ERANGE)
2546+
return DTERR_FIELD_OVERFLOW;
25262547
if (cp == str)
25272548
return DTERR_BAD_FORMAT;
25282549

@@ -2809,11 +2830,19 @@ DecodeTimezone(char *str, int *tzp)
28092830
if (*str != '+' && *str != '-')
28102831
return DTERR_BAD_FORMAT;
28112832

2833+
errno = 0;
28122834
hr = strtol(str + 1, &cp, 10);
2835+
if (errno == ERANGE)
2836+
return DTERR_TZDISP_OVERFLOW;
28132837

28142838
/* explicit delimiter? */
28152839
if (*cp == ':')
2840+
{
2841+
errno = 0;
28162842
min = strtol(cp + 1, &cp, 10);
2843+
if (errno == ERANGE)
2844+
return DTERR_TZDISP_OVERFLOW;
2845+
}
28172846
/* otherwise, might have run things together... */
28182847
else if (*cp == '\0' && strlen(str) > 3)
28192848
{
@@ -3056,7 +3085,10 @@ DecodeInterval(char **field, int *ftype, int nf, int *dtype, struct pg_tm * tm,
30563085

30573086
case DTK_DATE:
30583087
case DTK_NUMBER:
3088+
errno = 0;
30593089
val = strtol(field[i], &cp, 10);
3090+
if (errno == ERANGE)
3091+
return DTERR_FIELD_OVERFLOW;
30603092

30613093
if (type == IGNORE_DTF)
30623094
type = DTK_SECOND;

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