Skip to content

Commit a958b07

Browse files
committed
Reject "23:59:60.nnn" in datetime input.
It's intentional that we don't allow values greater than 24 hours, while we do allow "24:00:00" as well as "23:59:60" as inputs. However, the range check was miscoded in such a way that it would accept "23:59:60.nnn" with a nonzero fraction. For time or timetz, the stored result would then be greater than "24:00:00" which would fail dump/reload, not to mention possibly confusing other operations. Fix by explicitly calculating the result and making sure it does not exceed 24 hours. (This calculation is redundant with what will happen later in tm2time or tm2timetz. Maybe someday somebody will find that annoying enough to justify refactoring to avoid the duplication; but that seems too invasive for a back-patched bug fix, and the cost is probably unmeasurable anyway.) Note that this change also rejects such input as the time portion of a timestamp(tz) value. Back-patch to v10. The bug is far older, but to change this pre-v10 we'd need to ensure that the logic behaves sanely with float timestamps, which is possibly nontrivial due to roundoff considerations. Doesn't really seem worth troubling with. Per report from Christoph Berg. Discussion: https://postgr.es/m/20200520125807.GB296739@msg.df7cb.de
1 parent 03aa25b commit a958b07

File tree

8 files changed

+176
-38
lines changed

8 files changed

+176
-38
lines changed

src/backend/utils/adt/date.c

Lines changed: 63 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <ctype.h>
1919
#include <limits.h>
2020
#include <float.h>
21+
#include <math.h>
2122
#include <time.h>
2223

2324
#include "access/xact.h"
@@ -1211,6 +1212,65 @@ tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
12111212
return 0;
12121213
}
12131214

1215+
/* time_overflows()
1216+
* Check to see if a broken-down time-of-day is out of range.
1217+
*/
1218+
bool
1219+
time_overflows(int hour, int min, int sec, fsec_t fsec)
1220+
{
1221+
/* Range-check the fields individually. */
1222+
if (hour < 0 || hour > HOURS_PER_DAY ||
1223+
min < 0 || min >= MINS_PER_HOUR ||
1224+
sec < 0 || sec > SECS_PER_MINUTE ||
1225+
fsec < 0 || fsec > USECS_PER_SEC)
1226+
return true;
1227+
1228+
/*
1229+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1230+
* that the total time value doesn't exceed 24:00:00.
1231+
*/
1232+
if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1233+
+ sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1234+
return true;
1235+
1236+
return false;
1237+
}
1238+
1239+
/* float_time_overflows()
1240+
* Same, when we have seconds + fractional seconds as one "double" value.
1241+
*/
1242+
bool
1243+
float_time_overflows(int hour, int min, double sec)
1244+
{
1245+
/* Range-check the fields individually. */
1246+
if (hour < 0 || hour > HOURS_PER_DAY ||
1247+
min < 0 || min >= MINS_PER_HOUR)
1248+
return true;
1249+
1250+
/*
1251+
* "sec", being double, requires extra care. Cope with NaN, and round off
1252+
* before applying the range check to avoid unexpected errors due to
1253+
* imprecise input. (We assume rint() behaves sanely with infinities.)
1254+
*/
1255+
if (isnan(sec))
1256+
return true;
1257+
sec = rint(sec * USECS_PER_SEC);
1258+
if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1259+
return true;
1260+
1261+
/*
1262+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1263+
* that the total time value doesn't exceed 24:00:00. This must match the
1264+
* way that callers will convert the fields to a time.
1265+
*/
1266+
if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1267+
* USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1268+
return true;
1269+
1270+
return false;
1271+
}
1272+
1273+
12141274
/* time2tm()
12151275
* Convert time data type to POSIX time structure.
12161276
*
@@ -1315,20 +1375,16 @@ make_time(PG_FUNCTION_ARGS)
13151375
double sec = PG_GETARG_FLOAT8(2);
13161376
TimeADT time;
13171377

1318-
/* This should match the checks in DecodeTimeOnly */
1319-
if (tm_hour < 0 || tm_min < 0 || tm_min > MINS_PER_HOUR - 1 ||
1320-
sec < 0 || sec > SECS_PER_MINUTE ||
1321-
tm_hour > HOURS_PER_DAY ||
1322-
/* test for > 24:00:00 */
1323-
(tm_hour == HOURS_PER_DAY && (tm_min > 0 || sec > 0)))
1378+
/* Check for time overflow */
1379+
if (float_time_overflows(tm_hour, tm_min, sec))
13241380
ereport(ERROR,
13251381
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
13261382
errmsg("time field value out of range: %d:%02d:%02g",
13271383
tm_hour, tm_min, sec)));
13281384

13291385
/* This should match tm2time */
13301386
time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1331-
* USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
1387+
* USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
13321388

13331389
PG_RETURN_TIMEADT(time);
13341390
}

src/backend/utils/adt/datetime.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -939,14 +939,9 @@ DecodeDateTime(char **field, int *ftype, int nf,
939939
if (dterr)
940940
return dterr;
941941

942-
/*
943-
* Check upper limit on hours; other limits checked in
944-
* DecodeTime()
945-
*/
946-
/* test for > 24:00:00 */
947-
if (tm->tm_hour > HOURS_PER_DAY ||
948-
(tm->tm_hour == HOURS_PER_DAY &&
949-
(tm->tm_min > 0 || tm->tm_sec > 0 || *fsec > 0)))
942+
/* check for time overflow */
943+
if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec,
944+
*fsec))
950945
return DTERR_FIELD_OVERFLOW;
951946
break;
952947

@@ -2221,16 +2216,8 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
22212216
else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
22222217
tm->tm_hour += HOURS_PER_DAY / 2;
22232218

2224-
/*
2225-
* This should match the checks in make_timestamp_internal
2226-
*/
2227-
if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_min > MINS_PER_HOUR - 1 ||
2228-
tm->tm_sec < 0 || tm->tm_sec > SECS_PER_MINUTE ||
2229-
tm->tm_hour > HOURS_PER_DAY ||
2230-
/* test for > 24:00:00 */
2231-
(tm->tm_hour == HOURS_PER_DAY &&
2232-
(tm->tm_min > 0 || tm->tm_sec > 0 || *fsec > 0)) ||
2233-
*fsec < INT64CONST(0) || *fsec > USECS_PER_SEC)
2219+
/* check for time overflow */
2220+
if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec))
22342221
return DTERR_FIELD_OVERFLOW;
22352222

22362223
if ((fmask & DTK_TIME_M) != DTK_TIME_M)

src/backend/utils/adt/timestamp.c

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
#include "parser/scansup.h"
3333
#include "utils/array.h"
3434
#include "utils/builtins.h"
35+
#include "utils/date.h"
3536
#include "utils/datetime.h"
3637
#include "utils/float.h"
3738

@@ -567,26 +568,16 @@ make_timestamp_internal(int year, int month, int day,
567568

568569
date = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
569570

570-
/*
571-
* This should match the checks in DecodeTimeOnly, except that since we're
572-
* dealing with a float "sec" value, we also explicitly reject NaN. (An
573-
* infinity input should get rejected by the range comparisons, but we
574-
* can't be sure how those will treat a NaN.)
575-
*/
576-
if (hour < 0 || min < 0 || min > MINS_PER_HOUR - 1 ||
577-
isnan(sec) ||
578-
sec < 0 || sec > SECS_PER_MINUTE ||
579-
hour > HOURS_PER_DAY ||
580-
/* test for > 24:00:00 */
581-
(hour == HOURS_PER_DAY && (min > 0 || sec > 0)))
571+
/* Check for time overflow */
572+
if (float_time_overflows(hour, min, sec))
582573
ereport(ERROR,
583574
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
584575
errmsg("time field value out of range: %d:%02d:%02g",
585576
hour, min, sec)));
586577

587578
/* This should match tm2time */
588579
time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
589-
* USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
580+
* USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
590581

591582
result = date * USECS_PER_DAY + time;
592583
/* check for major overflow */

src/include/utils/date.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,5 +76,7 @@ extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
7676
extern TimeADT GetSQLLocalTime(int32 typmod);
7777
extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
7878
extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
79+
extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
80+
extern bool float_time_overflows(int hour, int min, double sec);
7981

8082
#endif /* DATE_H */

src/test/regress/expected/time.out

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,47 @@ SELECT f1 AS "Eight" FROM TIME_TBL WHERE f1 >= '00:00';
7373
15:36:39
7474
(10 rows)
7575

76+
-- Check edge cases
77+
SELECT '23:59:59.999999'::time;
78+
time
79+
-----------------
80+
23:59:59.999999
81+
(1 row)
82+
83+
SELECT '23:59:59.9999999'::time; -- rounds up
84+
time
85+
----------
86+
24:00:00
87+
(1 row)
88+
89+
SELECT '23:59:60'::time; -- rounds up
90+
time
91+
----------
92+
24:00:00
93+
(1 row)
94+
95+
SELECT '24:00:00'::time; -- allowed
96+
time
97+
----------
98+
24:00:00
99+
(1 row)
100+
101+
SELECT '24:00:00.01'::time; -- not allowed
102+
ERROR: date/time field value out of range: "24:00:00.01"
103+
LINE 1: SELECT '24:00:00.01'::time;
104+
^
105+
SELECT '23:59:60.01'::time; -- not allowed
106+
ERROR: date/time field value out of range: "23:59:60.01"
107+
LINE 1: SELECT '23:59:60.01'::time;
108+
^
109+
SELECT '24:01:00'::time; -- not allowed
110+
ERROR: date/time field value out of range: "24:01:00"
111+
LINE 1: SELECT '24:01:00'::time;
112+
^
113+
SELECT '25:00:00'::time; -- not allowed
114+
ERROR: date/time field value out of range: "25:00:00"
115+
LINE 1: SELECT '25:00:00'::time;
116+
^
76117
--
77118
-- TIME simple math
78119
--

src/test/regress/expected/timetz.out

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,47 @@ SELECT f1 AS "Ten" FROM TIMETZ_TBL WHERE f1 >= '00:00-07';
9090
15:36:39-04
9191
(12 rows)
9292

93+
-- Check edge cases
94+
SELECT '23:59:59.999999'::timetz;
95+
timetz
96+
--------------------
97+
23:59:59.999999-07
98+
(1 row)
99+
100+
SELECT '23:59:59.9999999'::timetz; -- rounds up
101+
timetz
102+
-------------
103+
24:00:00-07
104+
(1 row)
105+
106+
SELECT '23:59:60'::timetz; -- rounds up
107+
timetz
108+
-------------
109+
24:00:00-07
110+
(1 row)
111+
112+
SELECT '24:00:00'::timetz; -- allowed
113+
timetz
114+
-------------
115+
24:00:00-07
116+
(1 row)
117+
118+
SELECT '24:00:00.01'::timetz; -- not allowed
119+
ERROR: date/time field value out of range: "24:00:00.01"
120+
LINE 1: SELECT '24:00:00.01'::timetz;
121+
^
122+
SELECT '23:59:60.01'::timetz; -- not allowed
123+
ERROR: date/time field value out of range: "23:59:60.01"
124+
LINE 1: SELECT '23:59:60.01'::timetz;
125+
^
126+
SELECT '24:01:00'::timetz; -- not allowed
127+
ERROR: date/time field value out of range: "24:01:00"
128+
LINE 1: SELECT '24:01:00'::timetz;
129+
^
130+
SELECT '25:00:00'::timetz; -- not allowed
131+
ERROR: date/time field value out of range: "25:00:00"
132+
LINE 1: SELECT '25:00:00'::timetz;
133+
^
93134
--
94135
-- TIME simple math
95136
--

src/test/regress/sql/time.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,16 @@ SELECT f1 AS "None" FROM TIME_TBL WHERE f1 < '00:00';
3030

3131
SELECT f1 AS "Eight" FROM TIME_TBL WHERE f1 >= '00:00';
3232

33+
-- Check edge cases
34+
SELECT '23:59:59.999999'::time;
35+
SELECT '23:59:59.9999999'::time; -- rounds up
36+
SELECT '23:59:60'::time; -- rounds up
37+
SELECT '24:00:00'::time; -- allowed
38+
SELECT '24:00:00.01'::time; -- not allowed
39+
SELECT '23:59:60.01'::time; -- not allowed
40+
SELECT '24:01:00'::time; -- not allowed
41+
SELECT '25:00:00'::time; -- not allowed
42+
3343
--
3444
-- TIME simple math
3545
--

src/test/regress/sql/timetz.sql

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,16 @@ SELECT f1 AS "None" FROM TIMETZ_TBL WHERE f1 < '00:00-07';
3535

3636
SELECT f1 AS "Ten" FROM TIMETZ_TBL WHERE f1 >= '00:00-07';
3737

38+
-- Check edge cases
39+
SELECT '23:59:59.999999'::timetz;
40+
SELECT '23:59:59.9999999'::timetz; -- rounds up
41+
SELECT '23:59:60'::timetz; -- rounds up
42+
SELECT '24:00:00'::timetz; -- allowed
43+
SELECT '24:00:00.01'::timetz; -- not allowed
44+
SELECT '23:59:60.01'::timetz; -- not allowed
45+
SELECT '24:01:00'::timetz; -- not allowed
46+
SELECT '25:00:00'::timetz; -- not allowed
47+
3848
--
3949
-- TIME simple math
4050
--

0 commit comments

Comments
 (0)