pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
can insert timestamp value that can't be read
3+ messages / 2 participants
[nested] [flat]

* can insert timestamp value that can't be read
@ 2014-05-06 22:31 Jeff Davis <pgsql@j-davis.com>
  2014-05-07 05:00 ` Re: can insert timestamp value that can't be read Tom Lane <tgl@sss.pgh.pa.us>
  0 siblings, 1 reply; 3+ messages in thread

From: Jeff Davis @ 2014-05-06 22:31 UTC (permalink / raw)
  To: pgsql-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; }
^ permalink raw reply [nested|flat] 3+ messages in thread

* Re: can insert timestamp value that can't be read
  2014-05-06 22:31 can insert timestamp value that can't be read Jeff Davis <pgsql@j-davis.com>
@ 2014-05-07 05:00 ` Tom Lane <tgl@sss.pgh.pa.us>
  2014-05-08 02:34   ` Re: can insert timestamp value that can't be read Jeff Davis <pgsql@j-davis.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Tom Lane @ 2014-05-07 05:00 UTC (permalink / raw)
  To: Jeff Davis <pgsql@j-davis.com>; +Cc: pgsql-bugs

Jeff Davis <pgsql@j-davis.com> writes:
> 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.

This seems to be adding a heck of a lot of cycles (viz, replacing a
simple int64 or float8 addition with timestamp2tm then tm2timestamp)
to fix a corner case that will probably not matter to anyone anytime
in the next 200 thousand years, give or take a few millenia.  I'm okay
with the concept of detecting overflow here, but not at this price.
Can't we do a more direct overflow check, comparable to what ordinary
int64/float8 addition does?

Also, surely timestamp_mi_interval has got the identical issue, and
probably some other operators.

			regards, tom lane


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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: can insert timestamp value that can't be read
  2014-05-06 22:31 can insert timestamp value that can't be read Jeff Davis <pgsql@j-davis.com>
  2014-05-07 05:00 ` Re: can insert timestamp value that can't be read Tom Lane <tgl@sss.pgh.pa.us>
@ 2014-05-08 02:34   ` Jeff Davis <pgsql@j-davis.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Jeff Davis @ 2014-05-08 02:34 UTC (permalink / raw)
  To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-bugs

On Wed, 2014-05-07 at 01:00 -0400, Tom Lane wrote:
> Jeff Davis <pgsql@j-davis.com> writes:
> > 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.
> 
> This seems to be adding a heck of a lot of cycles (viz, replacing a
> simple int64 or float8 addition with timestamp2tm then tm2timestamp)
> to fix a corner case that will probably not matter to anyone anytime
> in the next 200 thousand years, give or take a few millenia.  I'm okay
> with the concept of detecting overflow here, but not at this price.
> Can't we do a more direct overflow check, comparable to what ordinary
> int64/float8 addition does?

It's a bit more complex than that, because a similar example of the bug
occurs without overflow when float timestamps are enabled. In
timestamp2tm(), the date can end up larger than INT_MAX.

In principle, I think the check in timestamp_pl_interval() can be quite
cheap, because we essentially just need to check for overflow and do a
bounds check. But it's hard to determine the precise valid bounds, and
it seems a little fragile.

I will see if I can extract the important parts of the validity check
into a new function (or macro). It would need to match the other checks
precisely though, otherwise this bug will persist.

> Also, surely timestamp_mi_interval has got the identical issue, and
> probably some other operators.

timestamp_mi_interval() calls timestamp_pl_interval(). I'll take a look
around for other similar problems though.

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



^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2014-05-08 02:34 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2014-05-06 22:31 can insert timestamp value that can't be read Jeff Davis <pgsql@j-davis.com>
2014-05-07 05:00 ` Tom Lane <tgl@sss.pgh.pa.us>
2014-05-08 02:34   ` Jeff Davis <pgsql@j-davis.com>

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