pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Jeff Davis <pgsql@j-davis.com>
To: pgsql-bugs@postgresql.org
Subject: can insert timestamp value that can't be read
Date: Tue, 06 May 2014 15:31:11 -0700
Message-ID: <1399415471.27807.49.camel@sussancws0025> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Example:

  => create table r(t timestamp);
  CREATE TABLE
  => insert into r values(
         timestamp '294277-01-09 00:00:00' + interval '36 hour'
       );
  INSERT 0 1
  => select * from r;
  ERROR:  timestamp out of range

(A similar case with different values exists when compiled with
--disable-integer-datetimes.)

It looks like timestamp[tz]_pl_interval() is not doing proper validation
in all paths.

Patch attached. I looked for other areas that might be affected, but
none jumped out.

The patch does not introduce any tests because I couldn't think of a
good way to test both integer and float timestamps properly.

Regards,
	Jeff Davis

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

Attachments:

timestamp_validation.patch (text/x-patch, 3.5K) download *** a/src/backend/utils/adt/timestamp.c --- b/src/backend/utils/adt/timestamp.c *************** *** 2923,2934 **** timestamp_pl_interval(PG_FUNCTION_ARGS) result = timestamp; else { if (span->month != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; - if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), --- 2923,2934 ---- result = timestamp; else { + struct pg_tm tt, + *tm = &tt; + fsec_t fsec; + if (span->month != 0) { if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), *************** *** 2958,2966 **** timestamp_pl_interval(PG_FUNCTION_ARGS) if (span->day != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; int julian; if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) --- 2958,2963 ---- *************** *** 2978,2984 **** timestamp_pl_interval(PG_FUNCTION_ARGS) errmsg("timestamp out of range"))); } ! timestamp += span->time; result = timestamp; } --- 2975,2997 ---- errmsg("timestamp out of range"))); } ! if (span->time != 0) ! { ! /* add time directly to timestamp */ ! timestamp += span->time; ! ! /* convert back and forth only for consistent error checking */ ! if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! ! if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! } ! result = timestamp; } *************** *** 3023,3034 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) result = timestamp; else { if (span->month != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; - if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), --- 3036,3047 ---- result = timestamp; else { + struct pg_tm tt, + *tm = &tt; + fsec_t fsec; + if (span->month != 0) { if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) ereport(ERROR, (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), *************** *** 3060,3068 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) if (span->day != 0) { - struct pg_tm tt, - *tm = &tt; - fsec_t fsec; int julian; if (timestamp2tm(timestamp, &tz, tm, &fsec, NULL, NULL) != 0) --- 3073,3078 ---- *************** *** 3082,3088 **** timestamptz_pl_interval(PG_FUNCTION_ARGS) errmsg("timestamp out of range"))); } ! timestamp += span->time; result = timestamp; } --- 3092,3114 ---- errmsg("timestamp out of range"))); } ! if (span->time != 0) ! { ! /* add time directly to timestamp */ ! timestamp += span->time; ! ! /* convert back and forth only for consistent error checking */ ! if (timestamp2tm(timestamp, NULL, tm, &fsec, NULL, NULL) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! ! if (tm2timestamp(tm, fsec, NULL, &timestamp) != 0) ! ereport(ERROR, ! (errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE), ! errmsg("timestamp out of range"))); ! } ! result = timestamp; }

view thread (3+ messages)  latest in thread

Message-ID: <1399415471.27807.49.camel@sussancws0025>
Permalink:  ../1399415471.27807.49.camel@sussancws0025/
Also on:    postgresql.org/message-id/1399415471.27807.49.camel@sussancws0025

 ·  · 

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: pgsql@j-davis.com
  Subject: Re: can insert timestamp value that can't be read
  In-Reply-To: <1399415471.27807.49.camel@sussancws0025>
  References: <1399415471.27807.49.camel@sussancws0025>

* 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