Skip to content

Commit 64579be

Browse files
committed
Fix some incorrect parsing of time with time zone strings
When parsing a timetz string with a dynamic timezone abbreviation or a timezone not specified, it was possible to generate incorrect timestamps based on a date which uses some non-initialized variables if the input string did not specify fully a date to parse. This is already checked when a full timezone spec is included in the input string, but the two other cases mentioned above missed the same checks. This gets fixed by generating an error as this input is invalid, or in short when a date is not fully specified. Valgrind was complaining about this problem. Bug: #15910 Author: Alexander Lakhin Discussion: https://postgr.es/m/[email protected] Backpatch-through: 9.4
1 parent 75c1921 commit 64579be

File tree

3 files changed

+21
-0
lines changed

3 files changed

+21
-0
lines changed

src/backend/utils/adt/datetime.c

+6
Original file line numberDiff line numberDiff line change
@@ -2280,6 +2280,9 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
22802280
GetCurrentDateTime(tmp);
22812281
else
22822282
{
2283+
/* a date has to be specified */
2284+
if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2285+
return DTERR_BAD_FORMAT;
22832286
tmp->tm_year = tm->tm_year;
22842287
tmp->tm_mon = tm->tm_mon;
22852288
tmp->tm_mday = tm->tm_mday;
@@ -2307,6 +2310,9 @@ DecodeTimeOnly(char **field, int *ftype, int nf,
23072310
GetCurrentDateTime(tmp);
23082311
else
23092312
{
2313+
/* a date has to be specified */
2314+
if ((fmask & DTK_DATE_M) != DTK_DATE_M)
2315+
return DTERR_BAD_FORMAT;
23102316
tmp->tm_year = tm->tm_year;
23112317
tmp->tm_mon = tm->tm_mon;
23122318
tmp->tm_mday = tm->tm_mday;

src/test/regress/expected/timetz.out

+10
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,16 @@ INSERT INTO TIMETZ_TBL VALUES ('15:36:39 America/New_York');
1919
ERROR: invalid input syntax for type time with time zone: "15:36:39 America/New_York"
2020
LINE 1: INSERT INTO TIMETZ_TBL VALUES ('15:36:39 America/New_York');
2121
^
22+
-- this should fail (timezone not specified without a date)
23+
INSERT INTO TIMETZ_TBL VALUES ('15:36:39 m2');
24+
ERROR: invalid input syntax for type time with time zone: "15:36:39 m2"
25+
LINE 1: INSERT INTO TIMETZ_TBL VALUES ('15:36:39 m2');
26+
^
27+
-- this should fail (dynamic timezone abbreviation without a date)
28+
INSERT INTO TIMETZ_TBL VALUES ('15:36:39 MSK m2');
29+
ERROR: invalid input syntax for type time with time zone: "15:36:39 MSK m2"
30+
LINE 1: INSERT INTO TIMETZ_TBL VALUES ('15:36:39 MSK m2');
31+
^
2232
SELECT f1 AS "Time TZ" FROM TIMETZ_TBL;
2333
Time TZ
2434
----------------

src/test/regress/sql/timetz.sql

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ INSERT INTO TIMETZ_TBL VALUES ('2003-03-07 15:36:39 America/New_York');
1919
INSERT INTO TIMETZ_TBL VALUES ('2003-07-07 15:36:39 America/New_York');
2020
-- this should fail (the timezone offset is not known)
2121
INSERT INTO TIMETZ_TBL VALUES ('15:36:39 America/New_York');
22+
-- this should fail (timezone not specified without a date)
23+
INSERT INTO TIMETZ_TBL VALUES ('15:36:39 m2');
24+
-- this should fail (dynamic timezone abbreviation without a date)
25+
INSERT INTO TIMETZ_TBL VALUES ('15:36:39 MSK m2');
26+
2227

2328
SELECT f1 AS "Time TZ" FROM TIMETZ_TBL;
2429

0 commit comments

Comments
 (0)