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

Commit86b6243

Browse files
committed
Detect Julian-date overflow in timestamp[tz]_pl_interval.
We perform addition of the days field of an interval viaarithmetic on the Julian-date representation of the timestamp's date.This step is subject to int32 overflow, and we also should not letthe Julian date become very negative, for fear of weird results fromj2date. (In the timestamptz case, allow a Julian date of -1 to pass,since it might convert back to zero after timezone rotation.)The additions of the months and microseconds fields could alsooverflow, of course. However, I believe we need no additionalchecks there; the existing range checks should catch such cases.The difficulty here is that j2date's magic modular arithmetic couldproduce something that looks like it's in-range.Per bug #18313 from Christian Maurer. This has been wrong fora long time, so back-patch to all supported branches.Discussion:https://postgr.es/m/18313-64d2c8952d81e84b@postgresql.org
1 parent67f7aaa commit86b6243

File tree

3 files changed

+29
-4
lines changed

3 files changed

+29
-4
lines changed

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

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2949,8 +2949,16 @@ timestamp_pl_interval(PG_FUNCTION_ARGS)
29492949
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
29502950
errmsg("timestamp out of range")));
29512951

2952-
/* Add days by converting to and from Julian */
2953-
julian=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)+span->day;
2952+
/*
2953+
* Add days by converting to and from Julian. We need an overflow
2954+
* check here since j2date expects a non-negative integer input.
2955+
*/
2956+
julian=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday);
2957+
if (pg_add_s32_overflow(julian,span->day,&julian)||
2958+
julian<0)
2959+
ereport(ERROR,
2960+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2961+
errmsg("timestamp out of range")));
29542962
j2date(julian,&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
29552963

29562964
if (tm2timestamp(tm,fsec,NULL,&timestamp)!=0)
@@ -3057,8 +3065,19 @@ timestamptz_pl_interval(PG_FUNCTION_ARGS)
30573065
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
30583066
errmsg("timestamp out of range")));
30593067

3060-
/* Add days by converting to and from Julian */
3061-
julian=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday)+span->day;
3068+
/*
3069+
* Add days by converting to and from Julian. We need an overflow
3070+
* check here since j2date expects a non-negative integer input.
3071+
* In practice though, it will give correct answers for small
3072+
* negative Julian dates; we should allow -1 to avoid
3073+
* timezone-dependent failures, as discussed in timestamp.h.
3074+
*/
3075+
julian=date2j(tm->tm_year,tm->tm_mon,tm->tm_mday);
3076+
if (pg_add_s32_overflow(julian,span->day,&julian)||
3077+
julian<-1)
3078+
ereport(ERROR,
3079+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3080+
errmsg("timestamp out of range")));
30623081
j2date(julian,&tm->tm_year,&tm->tm_mon,&tm->tm_mday);
30633082

30643083
tz=DetermineTimeZoneOffset(tm,session_timezone);

‎src/test/regress/expected/horology.out

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,8 @@ SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days'
373373
Sun Dec 31 00:00:00 294276
374374
(1 row)
375375

376+
SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
377+
ERROR: timestamp out of range
376378
SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
377379
106751991 Days
378380
------------------
@@ -633,6 +635,8 @@ SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS
633635
Fri Dec 31 23:59:59 1999 PST
634636
(1 row)
635637

638+
SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
639+
ERROR: timestamp out of range
636640
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
637641
True
638642
------

‎src/test/regress/sql/horology.sql

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ SELECT timestamp without time zone '1999-12-01' + interval '1 month - 1 second'
8686
SELECTtimestamp without time zone'Jan 1, 4713 BC'+ interval'106000000 days'AS"Feb 23, 285506";
8787
SELECTtimestamp without time zone'Jan 1, 4713 BC'+ interval'107000000 days'AS"Jan 20, 288244";
8888
SELECTtimestamp without time zone'Jan 1, 4713 BC'+ interval'109203489 days'AS"Dec 31, 294276";
89+
SELECTtimestamp without time zone'2000-01-01'- interval'2483590 days'AS"out of range";
8990
SELECTtimestamp without time zone'12/31/294276'-timestamp without time zone'12/23/1999'AS"106751991 Days";
9091

9192
-- Shorthand values
@@ -117,6 +118,7 @@ SELECT timestamp with time zone '1996-03-01' - interval '1 second' AS "Feb 29";
117118
SELECTtimestamp with time zone'1999-03-01'- interval'1 second'AS"Feb 28";
118119
SELECTtimestamp with time zone'2000-03-01'- interval'1 second'AS"Feb 29";
119120
SELECTtimestamp with time zone'1999-12-01'+ interval'1 month - 1 second'AS"Dec 31";
121+
SELECTtimestamp with time zone'2000-01-01'- interval'2483590 days'AS"out of range";
120122

121123
SELECT (timestamp with time zone'today'= (timestamp with time zone'yesterday'+ interval'1 day'))as"True";
122124
SELECT (timestamp with time zone'today'= (timestamp with time zone'tomorrow'- interval'1 day'))as"True";

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp