Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitb2c64f5

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 wouldaccept "23:59:60.nnn" with a nonzero fraction. For time or timetz,the stored result would then be greater than "24:00:00" which wouldfail dump/reload, not to mention possibly confusing other operations.Fix by explicitly calculating the result and making sure it does notexceed 24 hours. (This calculation is redundant with what will happenlater in tm2time or tm2timetz. Maybe someday somebody will find thatannoying enough to justify refactoring to avoid the duplication; butthat seems too invasive for a back-patched bug fix, and the cost isprobably unmeasurable anyway.)Note that this change also rejects such input as the time portionof a timestamp(tz) value.Back-patch to v10. The bug is far older, but to change this pre-v10we'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 parent5ed8b4a commitb2c64f5

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/hash.h"
@@ -1242,6 +1243,65 @@ tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
12421243
return0;
12431244
}
12441245

1246+
/* time_overflows()
1247+
* Check to see if a broken-down time-of-day is out of range.
1248+
*/
1249+
bool
1250+
time_overflows(inthour,intmin,intsec,fsec_tfsec)
1251+
{
1252+
/* Range-check the fields individually. */
1253+
if (hour<0||hour>HOURS_PER_DAY||
1254+
min<0||min >=MINS_PER_HOUR||
1255+
sec<0||sec>SECS_PER_MINUTE||
1256+
fsec<0||fsec>USECS_PER_SEC)
1257+
return true;
1258+
1259+
/*
1260+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1261+
* that the total time value doesn't exceed 24:00:00.
1262+
*/
1263+
if ((((((hour*MINS_PER_HOUR+min)*SECS_PER_MINUTE)
1264+
+sec)*USECS_PER_SEC)+fsec)>USECS_PER_DAY)
1265+
return true;
1266+
1267+
return false;
1268+
}
1269+
1270+
/* float_time_overflows()
1271+
* Same, when we have seconds + fractional seconds as one "double" value.
1272+
*/
1273+
bool
1274+
float_time_overflows(inthour,intmin,doublesec)
1275+
{
1276+
/* Range-check the fields individually. */
1277+
if (hour<0||hour>HOURS_PER_DAY||
1278+
min<0||min >=MINS_PER_HOUR)
1279+
return true;
1280+
1281+
/*
1282+
* "sec", being double, requires extra care. Cope with NaN, and round off
1283+
* before applying the range check to avoid unexpected errors due to
1284+
* imprecise input. (We assume rint() behaves sanely with infinities.)
1285+
*/
1286+
if (isnan(sec))
1287+
return true;
1288+
sec=rint(sec*USECS_PER_SEC);
1289+
if (sec<0||sec>SECS_PER_MINUTE*USECS_PER_SEC)
1290+
return true;
1291+
1292+
/*
1293+
* Because we allow, eg, hour = 24 or sec = 60, we must check separately
1294+
* that the total time value doesn't exceed 24:00:00. This must match the
1295+
* way that callers will convert the fields to a time.
1296+
*/
1297+
if (((((hour*MINS_PER_HOUR+min)*SECS_PER_MINUTE)
1298+
*USECS_PER_SEC)+ (int64)sec)>USECS_PER_DAY)
1299+
return true;
1300+
1301+
return false;
1302+
}
1303+
1304+
12451305
/* time2tm()
12461306
* Convert time data type to POSIX time structure.
12471307
*
@@ -1346,20 +1406,16 @@ make_time(PG_FUNCTION_ARGS)
13461406
doublesec=PG_GETARG_FLOAT8(2);
13471407
TimeADTtime;
13481408

1349-
/* This should match the checks in DecodeTimeOnly */
1350-
if (tm_hour<0||tm_min<0||tm_min>MINS_PER_HOUR-1||
1351-
sec<0||sec>SECS_PER_MINUTE||
1352-
tm_hour>HOURS_PER_DAY||
1353-
/* test for > 24:00:00 */
1354-
(tm_hour==HOURS_PER_DAY&& (tm_min>0||sec>0)))
1409+
/* Check for time overflow */
1410+
if (float_time_overflows(tm_hour,tm_min,sec))
13551411
ereport(ERROR,
13561412
(errcode(ERRCODE_DATETIME_FIELD_OVERFLOW),
13571413
errmsg("time field value out of range: %d:%02d:%02g",
13581414
tm_hour,tm_min,sec)));
13591415

13601416
/* This should match tm2time */
13611417
time= (((tm_hour*MINS_PER_HOUR+tm_min)*SECS_PER_MINUTE)
1362-
*USECS_PER_SEC)+rint(sec*USECS_PER_SEC);
1418+
*USECS_PER_SEC)+(int64)rint(sec*USECS_PER_SEC);
13631419

13641420
PG_RETURN_TIMEADT(time);
13651421
}

‎src/backend/utils/adt/datetime.c

Lines changed: 5 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,9 @@ DecodeDateTime(char **field, int *ftype, int nf,
961961
if (dterr)
962962
returndterr;
963963

964-
/*
965-
* Check upper limit on hours; other limits checked in
966-
* DecodeTime()
967-
*/
968-
/* test for > 24:00:00 */
969-
if (tm->tm_hour>HOURS_PER_DAY||
970-
(tm->tm_hour==HOURS_PER_DAY&&
971-
(tm->tm_min>0||tm->tm_sec>0||*fsec>0)))
964+
/* check for time overflow */
965+
if (time_overflows(tm->tm_hour,tm->tm_min,tm->tm_sec,
966+
*fsec))
972967
returnDTERR_FIELD_OVERFLOW;
973968
break;
974969

@@ -2258,16 +2253,8 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
22582253
elseif (mer==PM&&tm->tm_hour!=HOURS_PER_DAY /2)
22592254
tm->tm_hour+=HOURS_PER_DAY /2;
22602255

2261-
/*
2262-
* This should match the checks in make_timestamp_internal
2263-
*/
2264-
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
2265-
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE||
2266-
tm->tm_hour>HOURS_PER_DAY||
2267-
/* test for > 24:00:00 */
2268-
(tm->tm_hour==HOURS_PER_DAY&&
2269-
(tm->tm_min>0||tm->tm_sec>0||*fsec>0))||
2270-
*fsec<INT64CONST(0)||*fsec>USECS_PER_SEC)
2256+
/* check for time overflow */
2257+
if (time_overflows(tm->tm_hour,tm->tm_min,tm->tm_sec,*fsec))
22712258
returnDTERR_FIELD_OVERFLOW;
22722259

22732260
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
@@ -33,6 +33,7 @@
3333
#include"parser/scansup.h"
3434
#include"utils/array.h"
3535
#include"utils/builtins.h"
36+
#include"utils/date.h"
3637
#include"utils/datetime.h"
3738

3839
/*
@@ -572,26 +573,16 @@ make_timestamp_internal(int year, int month, int day,
572573

573574
date=date2j(tm.tm_year,tm.tm_mon,tm.tm_mday)-POSTGRES_EPOCH_JDATE;
574575

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

592583
/* This should match tm2time */
593584
time= (((hour*MINS_PER_HOUR+min)*SECS_PER_MINUTE)
594-
*USECS_PER_SEC)+rint(sec*USECS_PER_SEC);
585+
*USECS_PER_SEC)+(int64)rint(sec*USECS_PER_SEC);
595586

596587
result=date*USECS_PER_DAY+time;
597588
/* check for major overflow */

‎src/include/utils/date.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,5 +73,7 @@ extern void EncodeSpecialDate(DateADT dt, char *str);
7373
externDateADTGetSQLCurrentDate(void);
7474
externTimeTzADT*GetSQLCurrentTime(int32typmod);
7575
externTimeADTGetSQLLocalTime(int32typmod);
76+
externbooltime_overflows(inthour,intmin,intsec,fsec_tfsec);
77+
externboolfloat_time_overflows(inthour,intmin,doublesec);
7678

7779
#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 f1AS"Eight"FROM TIME_TBLWHERE 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 f1AS"Ten"FROM TIMETZ_TBLWHERE 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)

[8]ページ先頭

©2009-2025 Movatter.jp