Skip to content

Commit 530d0c5

Browse files
committed
Move DTK_ISODOW DTK_DOW and DTK_DOY to be type UNITS rather than
RESERV. RESERV is meant for tokens like "now" and having them in that category throws errors like these when used as an input date: stark=# SELECT 'doy'::timestamptz; ERROR: unexpected dtype 33 while parsing timestamptz "doy" LINE 1: SELECT 'doy'::timestamptz; ^ stark=# SELECT 'dow'::timestamptz; ERROR: unexpected dtype 32 while parsing timestamptz "dow" LINE 1: SELECT 'dow'::timestamptz; ^ Found by LLVM's Libfuzzer
1 parent 567acf2 commit 530d0c5

File tree

2 files changed

+43
-42
lines changed

2 files changed

+43
-42
lines changed

src/backend/utils/adt/datetime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,8 +104,8 @@ static const datetkn datetktbl[] = {
104104
{"d", UNITS, DTK_DAY}, /* "day of month" for ISO input */
105105
{"dec", MONTH, 12},
106106
{"december", MONTH, 12},
107-
{"dow", RESERV, DTK_DOW}, /* day of week */
108-
{"doy", RESERV, DTK_DOY}, /* day of year */
107+
{"dow", UNITS, DTK_DOW}, /* day of week */
108+
{"doy", UNITS, DTK_DOY}, /* day of year */
109109
{"dst", DTZMOD, SECS_PER_HOUR},
110110
{EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
111111
{"feb", MONTH, 2},
@@ -115,7 +115,7 @@ static const datetkn datetktbl[] = {
115115
{"h", UNITS, DTK_HOUR}, /* "hour" */
116116
{LATE, RESERV, DTK_LATE}, /* "infinity" reserved for "late time" */
117117
{INVALID, RESERV, DTK_INVALID}, /* "invalid" reserved for bad time */
118-
{"isodow", RESERV, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
118+
{"isodow", UNITS, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
119119
{"isoyear", UNITS, DTK_ISOYEAR}, /* year in terms of the ISO week date */
120120
{"j", UNITS, DTK_JULIAN},
121121
{"jan", MONTH, 1},

src/backend/utils/adt/timestamp.c

Lines changed: 40 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -3953,6 +3953,26 @@ timestamp_part(PG_FUNCTION_ARGS)
39533953
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
39543954
break;
39553955

3956+
case DTK_DOW:
3957+
case DTK_ISODOW:
3958+
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3959+
ereport(ERROR,
3960+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3961+
errmsg("timestamp out of range")));
3962+
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
3963+
if (val == DTK_ISODOW && result == 0)
3964+
result = 7;
3965+
break;
3966+
3967+
case DTK_DOY:
3968+
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
3969+
ereport(ERROR,
3970+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3971+
errmsg("timestamp out of range")));
3972+
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
3973+
- date2j(tm->tm_year, 1, 1) + 1);
3974+
break;
3975+
39563976
case DTK_TZ:
39573977
case DTK_TZ_MINUTE:
39583978
case DTK_TZ_HOUR:
@@ -3995,25 +4015,6 @@ timestamp_part(PG_FUNCTION_ARGS)
39954015
#endif
39964016
break;
39974017
}
3998-
case DTK_DOW:
3999-
case DTK_ISODOW:
4000-
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4001-
ereport(ERROR,
4002-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4003-
errmsg("timestamp out of range")));
4004-
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4005-
if (val == DTK_ISODOW && result == 0)
4006-
result = 7;
4007-
break;
4008-
4009-
case DTK_DOY:
4010-
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4011-
ereport(ERROR,
4012-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4013-
errmsg("timestamp out of range")));
4014-
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4015-
- date2j(tm->tm_year, 1, 1) + 1);
4016-
break;
40174018

40184019
default:
40194020
ereport(ERROR,
@@ -4187,6 +4188,26 @@ timestamptz_part(PG_FUNCTION_ARGS)
41874188
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
41884189
break;
41894190

4191+
case DTK_DOW:
4192+
case DTK_ISODOW:
4193+
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4194+
ereport(ERROR,
4195+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4196+
errmsg("timestamp out of range")));
4197+
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4198+
if (val == DTK_ISODOW && result == 0)
4199+
result = 7;
4200+
break;
4201+
4202+
case DTK_DOY:
4203+
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4204+
ereport(ERROR,
4205+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4206+
errmsg("timestamp out of range")));
4207+
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4208+
- date2j(tm->tm_year, 1, 1) + 1);
4209+
break;
4210+
41904211
default:
41914212
ereport(ERROR,
41924213
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -4208,26 +4229,6 @@ timestamptz_part(PG_FUNCTION_ARGS)
42084229
#endif
42094230
break;
42104231

4211-
case DTK_DOW:
4212-
case DTK_ISODOW:
4213-
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4214-
ereport(ERROR,
4215-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4216-
errmsg("timestamp out of range")));
4217-
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4218-
if (val == DTK_ISODOW && result == 0)
4219-
result = 7;
4220-
break;
4221-
4222-
case DTK_DOY:
4223-
if (timestamp2tm(timestamp, &tz, tm, &fsec, &tzn, NULL) != 0)
4224-
ereport(ERROR,
4225-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4226-
errmsg("timestamp out of range")));
4227-
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4228-
- date2j(tm->tm_year, 1, 1) + 1);
4229-
break;
4230-
42314232
default:
42324233
ereport(ERROR,
42334234
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

0 commit comments

Comments
 (0)