]> the.earth.li Git - onak.git/commitdiff
Switch to re-entrant versions of *time functions
authorJonathan McDowell <noodles@earth.li>
Sat, 2 Jan 2021 11:18:55 +0000 (11:18 +0000)
committerJonathan McDowell <noodles@earth.li>
Sat, 2 Jan 2021 11:18:55 +0000 (11:18 +0000)
We're not running these in a multi-threaded scenario at present, but it
makes sense to avoid them.

keydb/keydctl.c
keyindex.c
log.c

index 8712f6361d00978817f42f717989e3f545ebb18d..7a659ab1aaf9fc6eba6a6de836b46e2c97cd1534 100644 (file)
@@ -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");
index eeac56cac47d238c7c3c3e270a7178e535418095..e8d569a45b22de0d04e4ab4a9eae82af3acf1839 100644 (file)
@@ -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 45ec17e01bfef44420a52834034043594bfa5982..7c6f2eeee5312e3e6e5dfd21fc4b976db5d2bc15 100644 (file)
--- 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);