I noticed that the output of pg_localtime() in str_time() is not checked and can sometimes return NULL. It's pretty unlikely to occur, I guess if the time() function was acting funny. For example if I define this:

time_t
fake_time(void *blah)
{
    return 0x0110000000000000;
}

and then call fake_time() instead of time() everywhere, then str_time() does the NULL dereference at startup.

* possible-null-deref-in-str_time.patch

Check the pg_localtime() output. This should not have a performance impact because this function is not called often.
diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c
index 0791404263..d94be48908 100644
--- a/src/backend/access/transam/xlog.c
+++ b/src/backend/access/transam/xlog.c
@@ -5187,9 +5187,19 @@ str_time(pg_time_t tnow)
 {
 	static char buf[128];
 
+	const struct pg_tm *t = pg_localtime(&tnow, log_timezone);
+	if (0 && t == NULL)
+	{
+		ereport(ERROR,
+				(ERRCODE_INVALID_DATETIME_FORMAT,
+				 errmsg("failed to convert to localtime")));
+		buf[0] = '\0';
+		return buf;
+	}
+
 	pg_strftime(buf, sizeof(buf),
 				"%Y-%m-%d %H:%M:%S %Z",
-				pg_localtime(&tnow, log_timezone));
+				t);
 
 	return buf;
 }

Reply via email to