From 2078e384a3ac148b48973c0d67bce90d5792fe3b Mon Sep 17 00:00:00 2001
From: Heikki Linnakangas <heikki.linnakangas@iki.fi>
Date: Mon, 26 Oct 2009 16:13:11 +0000
Subject: [PATCH] Fix range check in date_recv that tried to limit accepted
 values to only those accepted by date_in(). I confused julian day numbers and
 number of days since the postgres epoch 2000-01-01 in the original patch.

I just noticed that it's still easy to get such out-of-range values into
the database using to_date or +- operators, but this patch doesn't do
anything about those functions.

Per report from James Pye.
---
 src/backend/utils/adt/date.c | 5 +++--
 src/include/utils/datetime.h | 4 ++--
 2 files changed, 5 insertions(+), 4 deletions(-)

diff --git a/src/backend/utils/adt/date.c b/src/backend/utils/adt/date.c
index 2b82c20efe7..9b929f7eb23 100644
--- a/src/backend/utils/adt/date.c
+++ b/src/backend/utils/adt/date.c
@@ -8,7 +8,7 @@
  *
  *
  * IDENTIFICATION
- *	  $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.148 2009/09/04 11:20:22 heikki Exp $
+ *	  $PostgreSQL: pgsql/src/backend/utils/adt/date.c,v 1.149 2009/10/26 16:13:11 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -208,7 +208,8 @@ date_recv(PG_FUNCTION_ARGS)
 	result = (DateADT) pq_getmsgint(buf, sizeof(DateADT));
 
 	/* Limit to the same range that date_in() accepts. */
-	if (result < 0 || result > JULIAN_MAX)
+	if (result < -POSTGRES_EPOCH_JDATE ||
+		result >= JULIAN_MAX - POSTGRES_EPOCH_JDATE)
 		ereport(ERROR,
 				(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
 				 errmsg("date out of range")));
diff --git a/src/include/utils/datetime.h b/src/include/utils/datetime.h
index 3b1f161de3a..913688d4540 100644
--- a/src/include/utils/datetime.h
+++ b/src/include/utils/datetime.h
@@ -9,7 +9,7 @@
  * Portions Copyright (c) 1996-2009, PostgreSQL Global Development Group
  * Portions Copyright (c) 1994, Regents of the University of California
  *
- * $PostgreSQL: pgsql/src/include/utils/datetime.h,v 1.76 2009/09/04 11:20:23 heikki Exp $
+ * $PostgreSQL: pgsql/src/include/utils/datetime.h,v 1.77 2009/10/26 16:13:11 heikki Exp $
  *
  *-------------------------------------------------------------------------
  */
@@ -262,7 +262,7 @@ extern const int day_tab[2][13];
   || (((m) == JULIAN_MINMONTH) && ((d) >= JULIAN_MINDAY))))) \
  && ((y) < JULIAN_MAXYEAR))
 
-#define JULIAN_MAX (2145031948) /* == date2j(JULIAN_MAXYEAR, 1 ,1) */
+#define JULIAN_MAX (2147483494)	/* == date2j(JULIAN_MAXYEAR, 1 ,1) */
 
 /* Julian-date equivalents of Day 0 in Unix and Postgres reckoning */
 #define UNIX_EPOCH_JDATE		2440588 /* == date2j(1970, 1, 1) */
-- 
GitLab