Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit a963283

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 f506704 commit a963283

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"
@@ -1270,6 +1271,65 @@ tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
12701271
return 0;
12711272
}
12721273

1274+
/* time_overflows()
1275+
* Check to see if a broken-down time-of-day is out of range.
1276+
*/
1277+
bool
1278+
time_overflows(int hour, int min, int sec, fsec_t fsec)
1279+
{
1280+
/* Range-check the fields individually. */
1281+
if (hour < 0 || hour > HOURS_PER_DAY ||
1282+
min < 0 || min >= MINS_PER_HOUR ||
1283+
sec < 0 || sec > SECS_PER_MINUTE ||
1284+
fsec < 0 || fsec > USECS_PER_SEC)
1285+
return true;
1286+
1287+
/*
1288+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1289+
* that the total time value doesn't exceed 24:00:00.
1290+
*/
1291+
if ((((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1292+
+ sec) * USECS_PER_SEC) + fsec) > USECS_PER_DAY)
1293+
return true;
1294+
1295+
return false;
1296+
}
1297+
1298+
/* float_time_overflows()
1299+
* Same, when we have seconds + fractional seconds as one "double" value.
1300+
*/
1301+
bool
1302+
float_time_overflows(int hour, int min, double sec)
1303+
{
1304+
/* Range-check the fields individually. */
1305+
if (hour < 0 || hour > HOURS_PER_DAY ||
1306+
min < 0 || min >= MINS_PER_HOUR)
1307+
return true;
1308+
1309+
/*
1310+
* "sec", being double, requires extra care. Cope with NaN, and round off
1311+
* before applying the range check to avoid unexpected errors due to
1312+
* imprecise input. (We assume rint() behaves sanely with infinities.)
1313+
*/
1314+
if (isnan(sec))
1315+
return true;
1316+
sec = rint(sec * USECS_PER_SEC);
1317+
if (sec < 0 || sec > SECS_PER_MINUTE * USECS_PER_SEC)
1318+
return true;
1319+
1320+
/*
1321+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1322+
* that the total time value doesn't exceed 24:00:00. This must match the
1323+
* way that callers will convert the fields to a time.
1324+
*/
1325+
if (((((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
1326+
* USECS_PER_SEC) + (int64) sec) > USECS_PER_DAY)
1327+
return true;
1328+
1329+
return false;
1330+
}
1331+
1332+
12731333
/* time2tm()
12741334
* Convert time data type to POSIX time structure.
12751335
*
@@ -1374,20 +1434,16 @@ make_time(PG_FUNCTION_ARGS)
13741434
double sec = PG_GETARG_FLOAT8(2);
13751435
TimeADT time;
13761436

1377-
/* This should match the checks in DecodeTimeOnly */
1378-
if (tm_hour < 0 || tm_min < 0 || tm_min > MINS_PER_HOUR - 1 ||
1379-
sec < 0 || sec > SECS_PER_MINUTE ||
1380-
tm_hour > HOURS_PER_DAY ||
1381-
/* test for > 24:00:00 */
1382-
(tm_hour == HOURS_PER_DAY && (tm_min > 0 || sec > 0)))
1437+
/* Check for time overflow */
1438+
if (float_time_overflows(tm_hour, tm_min, sec))
13831439
ereport(ERROR,
13841440
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
13851441
errmsg("time field value out of range: %d:%02d:%02g",
13861442
tm_hour, tm_min, sec)));
13871443

13881444
/* This should match tm2time */
13891445
time = (((tm_hour * MINS_PER_HOUR + tm_min) * SECS_PER_MINUTE)
1390-
* USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
1446+
* USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
13911447

13921448
PG_RETURN_TIMEADT(time);
13931449
}

src/backend/utils/adt/datetime.c

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

939-
/*
940-
* Check upper limit on hours; other limits checked in
941-
* DecodeTime()
942-
*/
943-
/* test for > 24:00:00 */
944-
if (tm->tm_hour > HOURS_PER_DAY ||
945-
(tm->tm_hour == HOURS_PER_DAY &&
946-
(tm->tm_min > 0 || tm->tm_sec > 0 || *fsec > 0)))
939+
/* check for time overflow */
940+
if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec,
941+
*fsec))
947942
return DTERR_FIELD_OVERFLOW;
948943
break;
949944

@@ -2218,16 +2213,8 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
22182213
else if (mer == PM && tm->tm_hour != HOURS_PER_DAY / 2)
22192214
tm->tm_hour += HOURS_PER_DAY / 2;
22202215

2221-
/*
2222-
* This should match the checks in make_timestamp_internal
2223-
*/
2224-
if (tm->tm_hour < 0 || tm->tm_min < 0 || tm->tm_min > MINS_PER_HOUR - 1 ||
2225-
tm->tm_sec < 0 || tm->tm_sec > SECS_PER_MINUTE ||
2226-
tm->tm_hour > HOURS_PER_DAY ||
2227-
/* test for > 24:00:00 */
2228-
(tm->tm_hour == HOURS_PER_DAY &&
2229-
(tm->tm_min > 0 || tm->tm_sec > 0 || *fsec > 0)) ||
2230-
*fsec < INT64CONST(0) || *fsec > USECS_PER_SEC)
2216+
/* check for time overflow */
2217+
if (time_overflows(tm->tm_hour, tm->tm_min, tm->tm_sec, *fsec))
22312218
return DTERR_FIELD_OVERFLOW;
22322219

22332220
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

@@ -581,26 +582,16 @@ make_timestamp_internal(int year, int month, int day,
581582

582583
date = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
583584

584-
/*
585-
* This should match the checks in DecodeTimeOnly, except that since we're
586-
* dealing with a float "sec" value, we also explicitly reject NaN. (An
587-
* infinity input should get rejected by the range comparisons, but we
588-
* can't be sure how those will treat a NaN.)
589-
*/
590-
if (hour < 0 || min < 0 || min > MINS_PER_HOUR - 1 ||
591-
isnan(sec) ||
592-
sec < 0 || sec > SECS_PER_MINUTE ||
593-
hour > HOURS_PER_DAY ||
594-
/* test for > 24:00:00 */
595-
(hour == HOURS_PER_DAY && (min > 0 || sec > 0)))
585+
/* Check for time overflow */
586+
if (float_time_overflows(hour, min, sec))
596587
ereport(ERROR,
597588
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
598589
errmsg("time field value out of range: %d:%02d:%02g",
599590
hour, min, sec)));
600591

601592
/* This should match tm2time */
602593
time = (((hour * MINS_PER_HOUR + min) * SECS_PER_MINUTE)
603-
* USECS_PER_SEC) + rint(sec * USECS_PER_SEC);
594+
* USECS_PER_SEC) + (int64) rint(sec * USECS_PER_SEC);
604595

605596
result = date * USECS_PER_DAY + time;
606597
/* check for major overflow */

src/include/utils/date.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ extern int time2tm(TimeADT time, struct pg_tm *tm, fsec_t *fsec);
8080
extern int timetz2tm(TimeTzADT *time, struct pg_tm *tm, fsec_t *fsec, int *tzp);
8181
extern int tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result);
8282
extern int tm2timetz(struct pg_tm *tm, fsec_t fsec, int tz, TimeTzADT *result);
83+
extern bool time_overflows(int hour, int min, int sec, fsec_t fsec);
84+
extern bool float_time_overflows(int hour, int min, double sec);
8385
extern void AdjustTimeForTypmod(TimeADT *time, int32 typmod);
8486

8587
#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)