Validation in to_date()

Started by Hitoshi Haradaabout 13 years ago2 messages
#1Hitoshi Harada
umi.tanuki@gmail.com
1 attachment(s)

to_date() doesn't check the date range, which results in unreadable
data like this.

foo=# create table t as select to_date('9999-12-10 BC', 'YYYY-MM-DD
BC')::timestamp;
SELECT 1
foo=# table t;
ERROR: timestamp out of range

Attached is to add IS_VALID_JULIAN() as we do like in date_in(). I
think to_date() should follow it because it is the entrance place to
check sanity.

Thanks,
--
Hitoshi Harada

Attachments:

to_date_check.patchapplication/octet-stream; name=to_date_check.patchDownload
diff --git a/src/backend/utils/adt/formatting.c b/src/backend/utils/adt/formatting.c
index 2aa6df1..7c9aee3 100644
--- a/src/backend/utils/adt/formatting.c
+++ b/src/backend/utils/adt/formatting.c
@@ -3332,6 +3332,12 @@ to_date(PG_FUNCTION_ARGS)
 
 	do_to_timestamp(date_txt, fmt, &tm, &fsec);
 
+	if (!IS_VALID_JULIAN(tm.tm_year, tm.tm_mon, tm.tm_mday))
+		ereport(ERROR,
+				(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
+				 errmsg("date out of range: \"%s\"",
+						text_to_cstring(date_txt))));
+
 	result = date2j(tm.tm_year, tm.tm_mon, tm.tm_mday) - POSTGRES_EPOCH_JDATE;
 
 	PG_RETURN_DATEADT(result);
#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Hitoshi Harada (#1)
Re: Validation in to_date()

Hitoshi Harada <umi.tanuki@gmail.com> writes:

to_date() doesn't check the date range, which results in unreadable
data like this.

foo=# create table t as select to_date('9999-12-10 BC', 'YYYY-MM-DD
BC')::timestamp;
SELECT 1
foo=# table t;
ERROR: timestamp out of range

Attached is to add IS_VALID_JULIAN() as we do like in date_in(). I
think to_date() should follow it because it is the entrance place to
check sanity.

This looks like a good idea, will commit.

regards, tom lane

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