1
0
mirror of https://git.libssh.org/projects/libssh.git synced 2025-12-08 03:42:12 +03:00

log.c: Use localtime_r() in current_timestring()

Use localtime_r() instead of the thread unsafe localtime().

Signed-off-by: Eshan Kelkar <eshankelkar@galorithm.com>
Reviewed-by: Jakub Jelen <jjelen@redhat.com>
This commit is contained in:
Eshan Kelkar
2024-08-13 17:22:19 +05:30
committed by Jakub Jelen
parent 0e756306f0
commit e0aa182e7e

View File

@@ -59,22 +59,22 @@ static int current_timestring(int hires, char *buf, size_t len)
{ {
char tbuf[64]; char tbuf[64];
struct timeval tv; struct timeval tv;
struct tm *tm = NULL; struct tm tm, *tm_ptr = NULL;
time_t t; time_t t;
gettimeofday(&tv, NULL); gettimeofday(&tv, NULL);
t = (time_t) tv.tv_sec; t = (time_t) tv.tv_sec;
tm = localtime(&t); tm_ptr = localtime_r(&t, &tm);
if (tm == NULL) { if (tm_ptr == NULL) {
return -1; return -1;
} }
if (hires) { if (hires) {
strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", tm); strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", &tm);
snprintf(buf, len, "%s.%06ld", tbuf, (long)tv.tv_usec); snprintf(buf, len, "%s.%06ld", tbuf, (long)tv.tv_usec);
} else { } else {
strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", tm); strftime(tbuf, sizeof(tbuf), "%Y/%m/%d %H:%M:%S", &tm);
snprintf(buf, len, "%s", tbuf); snprintf(buf, len, "%s", tbuf);
} }