pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Vitaly Burovoy <vitaly.burovoy@gmail.com>
To: Pantelis Theodosiou <ypercube@gmail.com>
Cc: web+postgresql@modin.io
Cc: pgsql-bugs@postgresql.org
Cc: pgsql-hackers@postgresql.org
Subject: Re: [BUGS][PATCH] BUG #14486: Inserting and selecting interval have different constraints
Date: Thu, 5 Jan 2017 08:07:54 -0800
Message-ID: <CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com> (raw)
In-Reply-To: <CAKOSWNmn9Kqq8wC0kjG6uh9jMV6WEAXtSVPd8pVCacTDg4yJ4A@mail.gmail.com>
References: <20170104150301.10134.9126@wrigleys.postgresql.org>
	<CAE3TBxzkGbTt7YEfZOJfCuOv21ZKaPZ2_=Nxe65m8Xc_oQn-rg@mail.gmail.com>
	<CAKOSWNmn9Kqq8wC0kjG6uh9jMV6WEAXtSVPd8pVCacTDg4yJ4A@mail.gmail.com>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-hackers>

On 1/5/17, Vitaly Burovoy <vitaly.burovoy@gmail.com> wrote:
> On 1/4/17, Pantelis Theodosiou <ypercube@gmail.com> wrote:
>> On Wed, Jan 4, 2017 at 3:03 PM, <web+postgresql@modin.io> wrote:
>>
>>> The following bug has been logged on the website:
>>>
>>> Bug reference:      14486
>>> Logged by:          Per Modin
>>> Email address:      web+postgresql@modin.io
>>> PostgreSQL version: 9.6.1
>>> Operating system:   Linux 303a92173594 4.8.15-moby #1 SMP Sat Dec 17 0
>>> Description:
>>>
>>> Found this bug in 9.4.8, tried it in docker towards psql 9.6.1 and it's
>>> in
>>> there as well. A minimum working example would be as follows:
>>>
>>> ```
>>> postgres=# CREATE TABLE tbl AS SELECT 9223372036854 * interval '1
>>> second'
>>> col; TABLE tbl;
>>> SELECT 1
>>> ERROR:  interval out of range
>>> ```
>>>
>>> ```
>>> postgres=# SELECT count(*) FROM tbl;
>>>  count
>>> -------
>>>      1
>>> (1 row)
>>> ```
>>>
>>> It seems that inserting and retrieving data have different constraints.
>>> As
>>> you
>>> can see from query 2, the data still gets inserted.
>>>
>>> ```
>>> postgres=# select version();
>>>                                          version
>>> ------------------------------------------------------------
>>> ------------------------------
>>>  PostgreSQL 9.6.1 on x86_64-pc-linux-gnu, compiled by gcc (Debian
>>> 4.9.2-10)
>>> 4.9.2, 64-bit
>>> (1 row)
>>> ```
>>>
>>> Best regards,
>>> Per Modin
>>>
>>>
>> And these work without error:
>>
>> postgres=# select col - 9223372036854 * interval '1 second' from tbl ;
>>  ?column?
>> ----------
>>  00:00:00
>> (1 row)
>>
>> postgres=# select col from xx where col < interval '100000 year' ;
>>  col
>> -----
>> (0 rows)
>>
>
> Yes, it is a bug, but it is not a constraint, it is just different
> internal checks.
> Moreover even if a function does not raise an error, output could be wrong
> (pay attention to the duplicated '-' sign)
> postgres=# SELECT '-2147483647:59:59'::interval - '1s'::interval;
>       ?column?
> --------------------
>  --2147483648:00:00
> (1 row)
>
> I've written a patch which fixes that bug (in attachment).
> Should it be registered in the CF?

Oops. Forgot to attach the patch. Fixed.

-- 
Best regards,
Vitaly Burovoy

-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Attachments:

0001-Add-check-for-overflow-to-interval-functions.patch (application/octet-stream, 7.3K) download From b418ee6809558f6119c76103f0361f73c2317821 Mon Sep 17 00:00:00 2001 From: Vitaly Burovoy <vitaly.burovoy@gmail.com> Date: Thu, 5 Jan 2017 15:52:38 +0000 Subject: [PATCH] Add check for overflow to 'interval' functions Before this commit arithmeric operators could lead to a state when interval_out raised an exception, i.e. you could not dump a table with a bad interval. --- src/backend/utils/adt/timestamp.c | 54 ++++++++++++++++++++++++++++++++-- src/test/regress/expected/interval.out | 22 ++++++++++++++ src/test/regress/sql/interval.sql | 20 +++++++++++++ 3 files changed, 94 insertions(+), 2 deletions(-) diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c index a87f982..53336a5 100644 --- a/src/backend/utils/adt/timestamp.c +++ b/src/backend/utils/adt/timestamp.c @@ -1034,6 +1034,29 @@ interval_out(PG_FUNCTION_ARGS) PG_RETURN_CSTRING(result); } +static void +interval_check_overflow(Interval *i) +{ + /* We compare hours with INT_MIN and INT_MAX because "int" type is used + * in interval_out (pg_tm->hour) and can lead to overflow. + * Also make bounds be the same in modulus to be able to use the unary '-' + * operator for negative intervals without possible UB. + */ +#ifdef HAVE_INT64_TIMESTAMP + if (i->time >= ( ((int64)INT_MAX + 1) * SECS_PER_HOUR * USECS_PER_SEC) || + i->time <= (-((int64)INT_MAX + 1) * SECS_PER_HOUR * USECS_PER_SEC)) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval out of range"))); +#else + if (i->time >= ( ((int64)INT_MAX + 1) * SECS_PER_HOUR) || + i->time <= (-((int64)INT_MAX + 1) * SECS_PER_HOUR)) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval out of range"))); +#endif +} + /* * interval_recv - converts external binary format to interval */ @@ -1058,6 +1081,8 @@ interval_recv(PG_FUNCTION_ARGS) interval->day = pq_getmsgint(buf, sizeof(interval->day)); interval->month = pq_getmsgint(buf, sizeof(interval->month)); + interval_check_overflow(interval); + AdjustIntervalForTypmod(interval, typmod); PG_RETURN_INTERVAL_P(interval); @@ -1610,6 +1635,9 @@ make_interval(PG_FUNCTION_ARGS) double secs = PG_GETARG_FLOAT8(6); Interval *result; + int32 years_month; + int32 weeks_days; + /* * Reject out-of-range inputs. We really ought to check the integer * inputs as well, but it's not entirely clear what limits to apply. @@ -1619,9 +1647,23 @@ make_interval(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("interval out of range"))); + years_month = years * MONTHS_PER_YEAR; + weeks_days = weeks * 7; + + if((years_month / MONTHS_PER_YEAR != years) || (weeks_days / 7 != weeks)) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval out of range"))); + result = (Interval *) palloc(sizeof(Interval)); - result->month = years * MONTHS_PER_YEAR + months; - result->day = weeks * 7 + days; + result->month = years_month + months; + result->day = weeks_days + days; + + if ((SAMESIGN(years_month, months) && !SAMESIGN(result->month, months)) || + (SAMESIGN(weeks_days, days) && !SAMESIGN(result->day, days))) + ereport(ERROR, + (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), + errmsg("interval out of range"))); secs += hours * (double) SECS_PER_HOUR + mins * (double) SECS_PER_MINUTE; @@ -1631,6 +1673,8 @@ make_interval(PG_FUNCTION_ARGS) result->time = secs; #endif + interval_check_overflow(result); + PG_RETURN_INTERVAL_P(result); } @@ -3383,6 +3427,8 @@ interval_pl(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("interval out of range"))); + interval_check_overflow(result); + PG_RETURN_INTERVAL_P(result); } @@ -3417,6 +3463,8 @@ interval_mi(PG_FUNCTION_ARGS) (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), errmsg("interval out of range"))); + interval_check_overflow(result); + PG_RETURN_INTERVAL_P(result); } @@ -3504,6 +3552,8 @@ interval_mul(PG_FUNCTION_ARGS) result->time = span->time * factor + sec_remainder; #endif + interval_check_overflow(result); + PG_RETURN_INTERVAL_P(result); } diff --git a/src/test/regress/expected/interval.out b/src/test/regress/expected/interval.out index 946c97a..9b85ad5 100644 --- a/src/test/regress/expected/interval.out +++ b/src/test/regress/expected/interval.out @@ -863,3 +863,25 @@ select make_interval(secs := 7e12); @ 1944444444 hours 26 mins 40 secs (1 row) +-- overflow check +-- To be sure an error is raised by an internal check (not output) function, INSERT is used instead of SELECT. +CREATE TABLE interval_chk(i interval); +INSERT INTO interval_chk VALUES(7730941132799 * interval '1 second'); -- ok, maximal allowed value +INSERT INTO interval_chk VALUES(7730941132800 * interval '1 second'); +ERROR: interval out of range +INSERT INTO interval_chk VALUES(-7730941132799 * interval '1 second'); -- ok, minimal allowed value +INSERT INTO interval_chk VALUES(-7730941132800 * interval '1 second'); +ERROR: interval out of range +INSERT INTO interval_chk VALUES('2147483647:59:59'::interval); -- OK +INSERT INTO interval_chk VALUES('-2147483647:59:59'::interval); -- OK +INSERT INTO interval_chk VALUES('2147483647:59:59'::interval + '1s'::interval); +ERROR: interval out of range +INSERT INTO interval_chk VALUES('-2147483647:59:59'::interval - '1s'::interval); +ERROR: interval out of range +INSERT INTO interval_chk VALUES(make_interval(hours => 2147483647, secs => 3599)); --OK +INSERT INTO interval_chk VALUES(make_interval(hours => 2147483647, secs => 3600)); +ERROR: interval out of range +INSERT INTO interval_chk VALUES(make_interval(hours => -2147483647, secs => -3599)); --OK +INSERT INTO interval_chk VALUES(make_interval(hours => -2147483647, secs => -3600)); +ERROR: interval out of range +DROP TABLE interval_chk; diff --git a/src/test/regress/sql/interval.sql b/src/test/regress/sql/interval.sql index cff9ada..843838a 100644 --- a/src/test/regress/sql/interval.sql +++ b/src/test/regress/sql/interval.sql @@ -281,3 +281,23 @@ select make_interval(months := 'NaN'::float::int); select make_interval(secs := 'inf'); select make_interval(secs := 'NaN'); select make_interval(secs := 7e12); + +-- overflow check +-- To be sure an error is raised by an internal check (not output) function, INSERT is used instead of SELECT. +CREATE TABLE interval_chk(i interval); +INSERT INTO interval_chk VALUES(7730941132799 * interval '1 second'); -- ok, maximal allowed value +INSERT INTO interval_chk VALUES(7730941132800 * interval '1 second'); +INSERT INTO interval_chk VALUES(-7730941132799 * interval '1 second'); -- ok, minimal allowed value +INSERT INTO interval_chk VALUES(-7730941132800 * interval '1 second'); + +INSERT INTO interval_chk VALUES('2147483647:59:59'::interval); -- OK +INSERT INTO interval_chk VALUES('-2147483647:59:59'::interval); -- OK + +INSERT INTO interval_chk VALUES('2147483647:59:59'::interval + '1s'::interval); +INSERT INTO interval_chk VALUES('-2147483647:59:59'::interval - '1s'::interval); + +INSERT INTO interval_chk VALUES(make_interval(hours => 2147483647, secs => 3599)); --OK +INSERT INTO interval_chk VALUES(make_interval(hours => 2147483647, secs => 3600)); +INSERT INTO interval_chk VALUES(make_interval(hours => -2147483647, secs => -3599)); --OK +INSERT INTO interval_chk VALUES(make_interval(hours => -2147483647, secs => -3600)); +DROP TABLE interval_chk; -- 2.10.2

view thread (10+ messages)  latest in thread

Message-ID: <CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com>
Permalink:  ../CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com/
Also on:    postgresql.org/message-id/CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com

 ·  · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-bugs@postgresql.org
  Cc: vitaly.burovoy@gmail.com, ypercube@gmail.com, web+postgresql@modin.io, pgsql-hackers@postgresql.org
  Subject: Re: [BUGS][PATCH] BUG #14486: Inserting and selecting interval have different constraints
  In-Reply-To: <CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com>
  References: <20170104150301.10134.9126@wrigleys.postgresql.org> <CAE3TBxzkGbTt7YEfZOJfCuOv21ZKaPZ2_=Nxe65m8Xc_oQn-rg@mail.gmail.com> <CAKOSWNmn9Kqq8wC0kjG6uh9jMV6WEAXtSVPd8pVCacTDg4yJ4A@mail.gmail.com> <CAKOSWNnhovGBeQfe9N-L09WW++zaZtg7p_2fq3MpTfUkqDinAA@mail.gmail.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox