From eba0d4cb6bae25cdf7946e7538bf388af7bc495f Mon Sep 17 00:00:00 2001 From: Guillem Jover Date: Fri, 26 Feb 2021 23:22:54 +0100 Subject: [PATCH] TT#111150 Use localtime_r() instead of thread-unsafe localtime() While the code is not threaded, better be future-proof and use safer APIs, which in addition has less side-effects as it does not set the global TZ related variables. Change-Id: I57fbe6683590cc2fbc0da508819553c040764739 Warned-by: lgtm --- medmysql.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/medmysql.c b/medmysql.c index d74cf64..43b879c 100644 --- a/medmysql.c +++ b/medmysql.c @@ -1192,19 +1192,25 @@ int medmysql_update_call_stat_info(const char *call_code, const double start_tim char period[STAT_PERIOD_SIZE]; time_t etime = (time_t)start_time; + struct tm etm; char period_key[STAT_PERIOD_SIZE+4]; struct medmysql_call_stat_info_t * period_t; + if (!localtime_r(&etime, &etm)) { + L_CRITICAL("Cannot get localtime: %s", strerror(errno)); + return -1; + } + switch (config_stats_period) { case MED_STATS_HOUR: - strftime(period, STAT_PERIOD_SIZE, "%Y-%m-%d %H:00:00", localtime(&etime)); + strftime(period, STAT_PERIOD_SIZE, "%Y-%m-%d %H:00:00", &etm); break; case MED_STATS_DAY: - strftime(period, STAT_PERIOD_SIZE, "%Y-%m-%d 00:00:00", localtime(&etime)); + strftime(period, STAT_PERIOD_SIZE, "%Y-%m-%d 00:00:00", &etm); break; case MED_STATS_MONTH: - strftime(period, STAT_PERIOD_SIZE, "%Y-%m-01 00:00:00", localtime(&etime)); + strftime(period, STAT_PERIOD_SIZE, "%Y-%m-01 00:00:00", &etm); break; default: L_CRITICAL("Undefinied or wrong config_stats_period %d",