Skip to content

Commit

Permalink
localtime is not threadsafe
Browse files Browse the repository at this point in the history
According to posix, localtime and gmtime are required to return the
same pointer. The implementation is a static glocal struct in glibc's
time/localtime. Use localtime_r instead.
  • Loading branch information
RH-steve-grubb committed Apr 25, 2024
1 parent 04c1dc3 commit a9a57c6
Showing 1 changed file with 4 additions and 8 deletions.
12 changes: 4 additions & 8 deletions src/library/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -72,18 +72,14 @@ void msg(int priority, const char *fmt, ...)
}

time_t rawtime;
struct tm *timeinfo;
struct tm timeinfo;
char buffer[80];

time(&rawtime);
timeinfo = localtime(&rawtime);
// localtime is not threadsafe, use _r version for safety
(void) localtime_r(&rawtime, &timeinfo);

if (timeinfo == NULL) {
fputs("Could not get localtime\n", stderr);
exit(EXIT_FAILURE);
}

strftime(buffer, sizeof(buffer), "%x %T [ ", timeinfo);
strftime(buffer, sizeof(buffer), "%x %T [ ", &timeinfo);
fputs(buffer, stderr);

fputs(color, stderr);
Expand Down

0 comments on commit a9a57c6

Please sign in to comment.