From 97be85bfba76b9ed0aa6ad01afc7c6efc4b370d5 Mon Sep 17 00:00:00 2001
From: Jonathan McDowell <noodles@earth.li>
Date: Sat, 2 Jan 2021 11:18:55 +0000
Subject: [PATCH] Switch to re-entrant versions of *time functions

We're not running these in a multi-threaded scenario at present, but it
makes sense to avoid them.
---
 keydb/keydctl.c |  3 ++-
 keyindex.c      | 27 ++++++++++++++-------------
 log.c           | 18 +++++++++---------
 3 files changed, 25 insertions(+), 23 deletions(-)

diff --git a/keydb/keydctl.c b/keydb/keydctl.c
index 8712f63..7a659ab 100644
--- a/keydb/keydctl.c
+++ b/keydb/keydctl.c
@@ -150,6 +150,7 @@ static void keyd_status(void)
 {
 	uint32_t reply;
 	struct keyd_stats stats;
+	char started[26];	/* ctime(3) says 26 is the necessary size */
 
 	if (keyd_do_command(KEYD_CMD_VERSION, &reply, sizeof(reply)) == -1) {
 		printf("Got failure asking for keyd version.\n");
@@ -162,7 +163,7 @@ static void keyd_status(void)
 		return;
 	}
 
-	printf("keyd running since %s", ctime(&stats.started));
+	printf("keyd running since %s", ctime_r(&stats.started, started));
 	printf("%d client connections received\n", stats.connects);
 
 	printf("Command statistics:\n");
diff --git a/keyindex.c b/keyindex.c
index eeac56c..e8d569a 100644
--- a/keyindex.c
+++ b/keyindex.c
@@ -231,7 +231,7 @@ int list_subkeys(struct onak_dbctx *dbctx,
 		struct openpgp_signedpacket_list *subkeys, bool verbose,
 		bool html)
 {
-	struct tm	*created = NULL;
+	struct tm	created;
 	time_t		created_time = 0;
 	int	 	type = 0;
 	int	 	length = 0;
@@ -244,7 +244,7 @@ int list_subkeys(struct onak_dbctx *dbctx,
 					(subkeys->packet->data[2] << 16) +
 					(subkeys->packet->data[3] << 8) +
 					subkeys->packet->data[4];
-			created = gmtime(&created_time);
+			gmtime_r(&created_time, &created);
 
 			switch (subkeys->packet->data[0]) {
 			case 2:
@@ -270,9 +270,9 @@ int list_subkeys(struct onak_dbctx *dbctx,
 				length,
 				pkalgo2char(type),
 				keyid,
-				created->tm_year + 1900,
-				created->tm_mon + 1,
-				created->tm_mday);
+				created.tm_year + 1900,
+				created.tm_mon + 1,
+				created.tm_mday);
 
 		}
 		if (verbose) {
@@ -348,13 +348,14 @@ int key_index(struct onak_dbctx *dbctx,
 			bool skshash, bool html)
 {
 	struct openpgp_signedpacket_list	*curuid = NULL;
-	struct tm				*created = NULL;
+	struct tm				 created;
 	time_t					 created_time = 0;
 	int					 type = 0;
 	int					 length = 0;
 	char					 buf[1024];
 	uint64_t				 keyid;
 
+
 	if (html) {
 		puts("<pre>");
 	}
@@ -364,7 +365,7 @@ int key_index(struct onak_dbctx *dbctx,
 					(keys->publickey->data[2] << 16) +
 					(keys->publickey->data[3] << 8) +
 					keys->publickey->data[4];
-		created = gmtime(&created_time);
+		gmtime_r(&created_time, &created);
 
 		switch (keys->publickey->data[0]) {
 		case 2:
@@ -393,17 +394,17 @@ int key_index(struct onak_dbctx *dbctx,
 				pkalgo2char(type),
 				keyid,
 				keyid,
-				created->tm_year + 1900,
-				created->tm_mon + 1,
-				created->tm_mday);
+				created.tm_year + 1900,
+				created.tm_mon + 1,
+				created.tm_mday);
 		} else {
 			printf("pub  %5d%c/0x%016" PRIX64 " %04d/%02d/%02d ",
 				length,
 				pkalgo2char(type),
 				keyid,
-				created->tm_year + 1900,
-				created->tm_mon + 1,
-				created->tm_mday);
+				created.tm_year + 1900,
+				created.tm_mon + 1,
+				created.tm_mday);
 		}
 
 		curuid = keys->uids;
diff --git a/log.c b/log.c
index 45ec17e..7c6f2ee 100644
--- a/log.c
+++ b/log.c
@@ -133,19 +133,19 @@ loglevels getlogthreshold(void)
  */
 static void vflog(FILE *logfile, const char *format, va_list ap)
 {
-	struct tm *timestamp = NULL;
-	time_t     timer = 0;
+	struct tm timestamp;
+	time_t    timer = 0;
 
 	timer = time(NULL);
-	timestamp = localtime(&timer);
+	localtime_r(&timer, &timestamp);
 
 	fprintf(logfile, "[%02d/%02d/%4d %02d:%02d:%02d] %s[%d]: ",
-			timestamp->tm_mday,
-			timestamp->tm_mon + 1,
-			timestamp->tm_year + 1900,
-			timestamp->tm_hour,
-			timestamp->tm_min,
-			timestamp->tm_sec,
+			timestamp.tm_mday,
+			timestamp.tm_mon + 1,
+			timestamp.tm_year + 1900,
+			timestamp.tm_hour,
+			timestamp.tm_min,
+			timestamp.tm_sec,
 			(logappname == NULL) ? "" : logappname,
 			getpid());
 	vfprintf(logfile, format, ap);
-- 
2.39.5