On Tue, Jun 30, 2015 at 9:45 PM, Michael Paquier
<michael.paqu...@gmail.com> wrote:
> timestamp2tm is called close to 40 times in the backend source code,
> returning -1 in case of failure. However, there are two places in datetime.c
> where we do not check for its return value: GetCurrentDateTime and
> GetCurrentTimeUsec.
> This does not really matter much in practice as the timestamp used is
> GetCurrentTransactionStartTimestamp(), but for correctness shouldn't we
> check for its return code and report ERRCODE_DATETIME_VALUE_OUT_OF_RANGE on
> error?
>
> Per se the patch attached.

And here is the patch.
(Thanks David R. for the poke).
-- 
Michael
diff --git a/src/backend/utils/adt/datetime.c b/src/backend/utils/adt/datetime.c
index 2a44b6e..fe0ba80 100644
--- a/src/backend/utils/adt/datetime.c
+++ b/src/backend/utils/adt/datetime.c
@@ -371,8 +371,11 @@ GetCurrentDateTime(struct pg_tm * tm)
 	int			tz;
 	fsec_t		fsec;
 
-	timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, &fsec,
-				 NULL, NULL);
+	if (timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, &fsec,
+					 NULL, NULL) != 0)
+		ereport(ERROR,
+				(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
+				 errmsg("timestamp out of range")));
 	/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
 }
 
@@ -387,8 +390,11 @@ GetCurrentTimeUsec(struct pg_tm * tm, fsec_t *fsec, int *tzp)
 {
 	int			tz;
 
-	timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, fsec,
-				 NULL, NULL);
+	if (timestamp2tm(GetCurrentTransactionStartTimestamp(), &tz, tm, fsec,
+					 NULL, NULL) != 0)
+		ereport(ERROR,
+				(errcode(ERRCODE_DATETIME_VALUE_OUT_OF_RANGE),
+				 errmsg("timestamp out of range")));
 	/* Note: don't pass NULL tzp to timestamp2tm; affects behavior */
 	if (tzp != NULL)
 		*tzp = tz;
-- 
Sent via pgsql-hackers mailing list (pgsql-hackers@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-hackers

Reply via email to