]> granicus.if.org Git - postgresql/commitdiff
Reject out-of-range dates in to_date().
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 14 Jan 2013 20:19:48 +0000 (15:19 -0500)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 14 Jan 2013 20:20:15 +0000 (15:20 -0500)
Dates outside the supported range could be entered, but would not print
reasonably, and operations such as conversion to timestamp wouldn't behave
sanely either.  Since this has the potential to result in undumpable table
data, it seems worth back-patching.

Hitoshi Harada

src/backend/utils/adt/formatting.c

index 1a52429624481df9da56d617b0f13d19191f2365..d69d27343b03cba4b4b60a7ab3eb71cc1dd90c65 100644 (file)
@@ -3303,6 +3303,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);