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

Commite6e3ee5

Browse files
committed
Detect more overflows in timestamp[tz]_pl_interval.
In commit25cd2d6 I (tgl) opined that "The additions of the monthsand microseconds fields could also overflow, of course. However,I believe we need no additional checks there; the existing rangechecks should catch such cases". This is demonstrably wrong howeverfor the microseconds field, and given that discovery it seems prudentto be paranoid about the months addition as well.Report and patch by Joseph Koshakow. As before, back-patch to allsupported branches. (However, the test case doesn't work beforev15 because we didn't allow wider-than-int32 numbers in intervalliterals. A variant test could probably be built that fits withinthat restriction, but it didn't seem worth the trouble.)Discussion:https://postgr.es/m/CAAvxfHf77sRHKoEzUw9_cMYSpbpNS2C+J_+8Dq4+0oi8iKopeA@mail.gmail.com
1 parent28a8cc4 commite6e3ee5

File tree

3 files changed

+22
-4
lines changed

3 files changed

+22
-4
lines changed

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

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2915,7 +2915,10 @@ timestamp_pl_interval(PG_FUNCTION_ARGS)
29152915
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
29162916
errmsg("timestamp out of range")));
29172917

2918-
tm->tm_mon+=span->month;
2918+
if (pg_add_s32_overflow(tm->tm_mon,span->month,&tm->tm_mon))
2919+
ereport(ERROR,
2920+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2921+
errmsg("timestamp out of range")));
29192922
if (tm->tm_mon>MONTHS_PER_YEAR)
29202923
{
29212924
tm->tm_year+= (tm->tm_mon-1) /MONTHS_PER_YEAR;
@@ -2967,7 +2970,10 @@ timestamp_pl_interval(PG_FUNCTION_ARGS)
29672970
errmsg("timestamp out of range")));
29682971
}
29692972

2970-
timestamp+=span->time;
2973+
if (pg_add_s64_overflow(timestamp,span->time,&timestamp))
2974+
ereport(ERROR,
2975+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
2976+
errmsg("timestamp out of range")));
29712977

29722978
if (!IS_VALID_TIMESTAMP(timestamp))
29732979
ereport(ERROR,
@@ -3029,7 +3035,10 @@ timestamptz_pl_interval(PG_FUNCTION_ARGS)
30293035
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
30303036
errmsg("timestamp out of range")));
30313037

3032-
tm->tm_mon+=span->month;
3038+
if (pg_add_s32_overflow(tm->tm_mon,span->month,&tm->tm_mon))
3039+
ereport(ERROR,
3040+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3041+
errmsg("timestamp out of range")));
30333042
if (tm->tm_mon>MONTHS_PER_YEAR)
30343043
{
30353044
tm->tm_year+= (tm->tm_mon-1) /MONTHS_PER_YEAR;
@@ -3088,7 +3097,10 @@ timestamptz_pl_interval(PG_FUNCTION_ARGS)
30883097
errmsg("timestamp out of range")));
30893098
}
30903099

3091-
timestamp+=span->time;
3100+
if (pg_add_s64_overflow(timestamp,span->time,&timestamp))
3101+
ereport(ERROR,
3102+
(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
3103+
errmsg("timestamp out of range")));
30923104

30933105
if (!IS_VALID_TIMESTAMP(timestamp))
30943106
ereport(ERROR,

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,8 @@ SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '109203489 days'
375375

376376
SELECT timestamp without time zone '2000-01-01' - interval '2483590 days' AS "out of range";
377377
ERROR: timestamp out of range
378+
SELECT timestamp without time zone '294276-12-31 23:59:59' + interval '9223372036854775807 microseconds' AS "out of range";
379+
ERROR: timestamp out of range
378380
SELECT timestamp without time zone '12/31/294276' - timestamp without time zone '12/23/1999' AS "106751991 Days";
379381
106751991 Days
380382
------------------
@@ -637,6 +639,8 @@ SELECT timestamp with time zone '1999-12-01' + interval '1 month - 1 second' AS
637639

638640
SELECT timestamp with time zone '2000-01-01' - interval '2483590 days' AS "out of range";
639641
ERROR: timestamp out of range
642+
SELECT timestamp with time zone '294276-12-31 23:59:59 UTC' + interval '9223372036854775807 microseconds' AS "out of range";
643+
ERROR: timestamp out of range
640644
SELECT (timestamp with time zone 'today' = (timestamp with time zone 'yesterday' + interval '1 day')) as "True";
641645
True
642646
------

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ SELECT timestamp without time zone 'Jan 1, 4713 BC' + interval '106000000 days'
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";
8989
SELECTtimestamp without time zone'2000-01-01'- interval'2483590 days'AS"out of range";
90+
SELECTtimestamp without time zone'294276-12-31 23:59:59'+ interval'9223372036854775807 microseconds'AS"out of range";
9091
SELECTtimestamp without time zone'12/31/294276'-timestamp without time zone'12/23/1999'AS"106751991 Days";
9192

9293
-- Shorthand values
@@ -119,6 +120,7 @@ SELECT timestamp with time zone '1999-03-01' - interval '1 second' AS "Feb 28";
119120
SELECTtimestamp with time zone'2000-03-01'- interval'1 second'AS"Feb 29";
120121
SELECTtimestamp with time zone'1999-12-01'+ interval'1 month - 1 second'AS"Dec 31";
121122
SELECTtimestamp with time zone'2000-01-01'- interval'2483590 days'AS"out of range";
123+
SELECTtimestamp with time zone'294276-12-31 23:59:59 UTC'+ interval'9223372036854775807 microseconds'AS"out of range";
122124

123125
SELECT (timestamp with time zone'today'= (timestamp with time zone'yesterday'+ interval'1 day'))as"True";
124126
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