Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7AHo-0004RE-1v for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Jan 2014 20:57:04 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.80) (envelope-from ) id 1W7AHn-0002Lr-6q for pgsql-bugs@arkaria.postgresql.org; Sat, 25 Jan 2014 20:57:03 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7AHm-0002Li-5E for pgsql-bugs@postgresql.org; Sat, 25 Jan 2014 20:57:02 +0000 Received: from momjian.us ([72.94.173.45]) by magus.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1W7AHe-0003I9-Gr for pgsql-bugs@postgresql.org; Sat, 25 Jan 2014 20:57:01 +0000 Received: from bruce by momjian.us with local (Exim 4.72) (envelope-from ) id 1W7AHc-00053x-Vq; Sat, 25 Jan 2014 15:56:52 -0500 Date: Sat, 25 Jan 2014 15:56:52 -0500 From: Bruce Momjian To: rok.kralj@gmail.com Cc: pgsql-bugs@postgresql.org Subject: Re: BUG #8230: Time interval size overflow Message-ID: <20140125205652.GC9750@momjian.us> References: MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="7ZAtKRhVyVSsbBD2" Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.20 (2009-06-14) X-Pg-Spam-Score: -2.5 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org --7ZAtKRhVyVSsbBD2 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline 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 http://momjian.us EnterpriseDB http://enterprisedb.com + Everyone has their own god. + --7ZAtKRhVyVSsbBD2 Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="interval.diff" 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)) + --7ZAtKRhVyVSsbBD2 Content-Type: text/plain Content-Disposition: inline Content-Transfer-Encoding: 8bit MIME-Version: 1.0 -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs --7ZAtKRhVyVSsbBD2--