Skip to content

Commit dd04d43

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 e9bacfc commit dd04d43

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

src/backend/utils/adt/datetime.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,8 +106,8 @@ static const datetkn datetktbl[] = {
106106
{"d", UNITS, DTK_DAY}, /* "day of month" for ISO input */
107107
{"dec", MONTH, 12},
108108
{"december", MONTH, 12},
109-
{"dow", RESERV, DTK_DOW}, /* day of week */
110-
{"doy", RESERV, DTK_DOY}, /* day of year */
109+
{"dow", UNITS, DTK_DOW}, /* day of week */
110+
{"doy", UNITS, DTK_DOY}, /* day of year */
111111
{"dst", DTZMOD, SECS_PER_HOUR},
112112
{EPOCH, RESERV, DTK_EPOCH}, /* "epoch" reserved for system epoch time */
113113
{"feb", MONTH, 2},
@@ -117,7 +117,7 @@ static const datetkn datetktbl[] = {
117117
{"h", UNITS, DTK_HOUR}, /* "hour" */
118118
{LATE, RESERV, DTK_LATE}, /* "infinity" reserved for "late time" */
119119
{INVALID, RESERV, DTK_INVALID}, /* "invalid" reserved for bad time */
120-
{"isodow", RESERV, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
120+
{"isodow", UNITS, DTK_ISODOW}, /* ISO day of week, Sunday == 7 */
121121
{"isoyear", UNITS, DTK_ISOYEAR}, /* year in terms of the ISO week date */
122122
{"j", UNITS, DTK_JULIAN},
123123
{"jan", MONTH, 1},

src/backend/utils/adt/timestamp.c

Lines changed: 40 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -4106,6 +4106,26 @@ timestamp_part(PG_FUNCTION_ARGS)
41064106
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
41074107
break;
41084108

4109+
case DTK_DOW:
4110+
case DTK_ISODOW:
4111+
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4112+
ereport(ERROR,
4113+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4114+
errmsg("timestamp out of range")));
4115+
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4116+
if (val == DTK_ISODOW && result == 0)
4117+
result = 7;
4118+
break;
4119+
4120+
case DTK_DOY:
4121+
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4122+
ereport(ERROR,
4123+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4124+
errmsg("timestamp out of range")));
4125+
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4126+
- date2j(tm->tm_year, 1, 1) + 1);
4127+
break;
4128+
41094129
case DTK_TZ:
41104130
case DTK_TZ_MINUTE:
41114131
case DTK_TZ_HOUR:
@@ -4129,26 +4149,6 @@ timestamp_part(PG_FUNCTION_ARGS)
41294149
#endif
41304150
break;
41314151

4132-
case DTK_DOW:
4133-
case DTK_ISODOW:
4134-
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4135-
ereport(ERROR,
4136-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4137-
errmsg("timestamp out of range")));
4138-
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4139-
if (val == DTK_ISODOW && result == 0)
4140-
result = 7;
4141-
break;
4142-
4143-
case DTK_DOY:
4144-
if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0)
4145-
ereport(ERROR,
4146-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4147-
errmsg("timestamp out of range")));
4148-
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4149-
- date2j(tm->tm_year, 1, 1) + 1);
4150-
break;
4151-
41524152
default:
41534153
ereport(ERROR,
41544154
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -4320,6 +4320,26 @@ timestamptz_part(PG_FUNCTION_ARGS)
43204320
result = date2isoyear(tm->tm_year, tm->tm_mon, tm->tm_mday);
43214321
break;
43224322

4323+
case DTK_DOW:
4324+
case DTK_ISODOW:
4325+
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
4326+
ereport(ERROR,
4327+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4328+
errmsg("timestamp out of range")));
4329+
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4330+
if (val == DTK_ISODOW && result == 0)
4331+
result = 7;
4332+
break;
4333+
4334+
case DTK_DOY:
4335+
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
4336+
ereport(ERROR,
4337+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4338+
errmsg("timestamp out of range")));
4339+
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4340+
- date2j(tm->tm_year, 1, 1) + 1);
4341+
break;
4342+
43234343
default:
43244344
ereport(ERROR,
43254345
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -4341,26 +4361,6 @@ timestamptz_part(PG_FUNCTION_ARGS)
43414361
#endif
43424362
break;
43434363

4344-
case DTK_DOW:
4345-
case DTK_ISODOW:
4346-
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
4347-
ereport(ERROR,
4348-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4349-
errmsg("timestamp out of range")));
4350-
result = j2day(date2j(tm->tm_year, tm->tm_mon, tm->tm_mday));
4351-
if (val == DTK_ISODOW && result == 0)
4352-
result = 7;
4353-
break;
4354-
4355-
case DTK_DOY:
4356-
if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0)
4357-
ereport(ERROR,
4358-
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
4359-
errmsg("timestamp out of range")));
4360-
result = (date2j(tm->tm_year, tm->tm_mon, tm->tm_mday)
4361-
- date2j(tm->tm_year, 1, 1) + 1);
4362-
break;
4363-
43644364
default:
43654365
ereport(ERROR,
43664366
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),

0 commit comments

Comments
 (0)