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

Commit6490376

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 parentb41a85f commit6490376

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"
@@ -1268,6 +1269,65 @@ tm2time(struct pg_tm *tm, fsec_t fsec, TimeADT *result)
12681269
return0;
12691270
}
12701271

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

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

13861442
/* This should match tm2time */
13871443
time= (((tm_hour*MINS_PER_HOUR+tm_min)*SECS_PER_MINUTE)
1388-
*USECS_PER_SEC)+rint(sec*USECS_PER_SEC);
1444+
*USECS_PER_SEC)+(int64)rint(sec*USECS_PER_SEC);
13891445

13901446
PG_RETURN_TIMEADT(time);
13911447
}

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

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

948-
/*
949-
* Check upper limit on hours; other limits checked in
950-
* DecodeTime()
951-
*/
952-
/* test for > 24:00:00 */
953-
if (tm->tm_hour>HOURS_PER_DAY||
954-
(tm->tm_hour==HOURS_PER_DAY&&
955-
(tm->tm_min>0||tm->tm_sec>0||*fsec>0)))
948+
/* check for time overflow */
949+
if (time_overflows(tm->tm_hour,tm->tm_min,tm->tm_sec,
950+
*fsec))
956951
returnDTERR_FIELD_OVERFLOW;
957952
break;
958953

@@ -2242,16 +2237,8 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
22422237
elseif (mer==PM&&tm->tm_hour!=HOURS_PER_DAY /2)
22432238
tm->tm_hour+=HOURS_PER_DAY /2;
22442239

2245-
/*
2246-
* This should match the checks in make_timestamp_internal
2247-
*/
2248-
if (tm->tm_hour<0||tm->tm_min<0||tm->tm_min>MINS_PER_HOUR-1||
2249-
tm->tm_sec<0||tm->tm_sec>SECS_PER_MINUTE||
2250-
tm->tm_hour>HOURS_PER_DAY||
2251-
/* test for > 24:00:00 */
2252-
(tm->tm_hour==HOURS_PER_DAY&&
2253-
(tm->tm_min>0||tm->tm_sec>0||*fsec>0))||
2254-
*fsec<INT64CONST(0)||*fsec>USECS_PER_SEC)
2240+
/* check for time overflow */
2241+
if (time_overflows(tm->tm_hour,tm->tm_min,tm->tm_sec,*fsec))
22552242
returnDTERR_FIELD_OVERFLOW;
22562243

22572244
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
@@ -76,5 +76,7 @@ extern TimeTzADT *GetSQLCurrentTime(int32 typmod);
7676
externTimeADTGetSQLLocalTime(int32typmod);
7777
externinttime2tm(TimeADTtime,structpg_tm*tm,fsec_t*fsec);
7878
externinttimetz2tm(TimeTzADT*time,structpg_tm*tm,fsec_t*fsec,int*tzp);
79+
externbooltime_overflows(inthour,intmin,intsec,fsec_tfsec);
80+
externboolfloat_time_overflows(inthour,intmin,doublesec);
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 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