pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Bruce Momjian <bruce@momjian.us>
To: rok.kralj@gmail.com
Cc: pgsql-bugs@postgresql.org
Subject: Re: BUG #8230: Time interval size overflow
Date: Sat, 25 Jan 2014 15:56:52 -0500
Message-ID: <20140125205652.GC9750@momjian.us> (raw)
In-Reply-To: <E1UoAkT-0006qe-E5@wrigleys.postgresql.org>
References: <E1UoAkT-0006qe-E5@wrigleys.postgresql.org>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

On Sun, Jun 16, 2013 at 11:03:53AM +0000, rok.kralj@gmail.com wrote:
> The following bug has been logged on the website:
> 
> Bug reference:      8230
> Logged by:          Rok Kralj
> Email address:      rok.kralj@gmail.com
> PostgreSQL version: 9.2.4
> Operating system:   Arch Linux
> Description:        
> 
> As far as I know, the INTERVAL type is represented using three int32, one
> for months, days and milliseconds.
> 
> However, overflow detections seems to work correctly only on these three
> types; if they get overflown indirectly, they do not get detected:
> 
> SELECT INTERVAL '3000000000 days'
> ERROR:  interval field value out of range: "3000000000 days"
> 
> SELECT INTERVAL '2000000000 years'
> -147483648 years
> 
> I'm surprised this hasn't been reported yet. The expected behaviour is to
> raise an error.

The attached patch fixes the problem:

	test=> SELECT INTERVAL '2000000000 years';
	ERROR:  interval out of range
	LINE 1: SELECT INTERVAL '2000000000 years';

It also fixes this case

	test=> SELECT INTERVAL '2000000000-3 years';
	ERROR:  interval field value out of range: "2000000000-3 years"
	LINE 1: SELECT INTERVAL '2000000000-3 years';

-- 
  Bruce Momjian  <bruce@momjian.us>        http://momjian.us
  EnterpriseDB                             http://enterprisedb.com

  + Everyone has their own god. +

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

Attachments:

interval.diff (text/x-diff, 1.6K) download diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c new file mode 100644 index a61b40e..ae4e9e7 *** a/src/backend/utils/adt/datetime.c --- b/src/backend/utils/adt/datetime.c *************** DecodeInterval(char **field, int *ftype, *** 2976,2981 **** --- 2976,2983 ---- type = DTK_MONTH; if (*field[i] == '-') val2 = -val2; + if (((float)val * MONTHS_PER_YEAR + val2) > INT_MAX) + return DTERR_FIELD_OVERFLOW; val = val * MONTHS_PER_YEAR + val2; fval = 0; } diff --git a/src/backend/utils/adt/timestamp.c b/src/backend/utils/adt/timestamp.c new file mode 100644 index 4581862..a751593 *** a/src/backend/utils/adt/timestamp.c --- b/src/backend/utils/adt/timestamp.c *************** recalc: *** 1725,1730 **** --- 1725,1732 ---- int tm2interval(struct pg_tm * tm, fsec_t fsec, Interval *span) { + if ((float)tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon > INT_MAX) + return -1; span->month = tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon; span->day = tm->tm_mday; #ifdef HAVE_INT64_TIMESTAMP diff --git a/src/interfaces/ecpg/pgtypeslib/interval.c b/src/interfaces/ecpg/pgtypeslib/interval.c new file mode 100644 index efa775d..e1307e3 *** a/src/interfaces/ecpg/pgtypeslib/interval.c --- b/src/interfaces/ecpg/pgtypeslib/interval.c *************** recalc: *** 1036,1041 **** --- 1036,1043 ---- static int tm2interval(struct tm * tm, fsec_t fsec, interval * span) { + if ((float)tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon > INT_MAX) + return -1; span->month = tm->tm_year * MONTHS_PER_YEAR + tm->tm_mon; #ifdef HAVE_INT64_TIMESTAMP span->time = (((((((tm->tm_mday * INT64CONST(24)) +

view thread (4+ messages)  latest in thread

Message-ID: <20140125205652.GC9750@momjian.us>
Permalink:  ../20140125205652.GC9750@momjian.us/
Also on:    postgresql.org/message-id/20140125205652.GC9750@momjian.us

 ·  · 

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: bruce@momjian.us, rok.kralj@gmail.com
  Subject: Re: BUG #8230: Time interval size overflow
  In-Reply-To: <20140125205652.GC9750@momjian.us>
  References: <E1UoAkT-0006qe-E5@wrigleys.postgresql.org> <20140125205652.GC9750@momjian.us>

* 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