From 753ca2b9341b39d81e474065b92a958ad2aca021 Mon Sep 17 00:00:00 2001 From: Joel Rosdahl Date: Sun, 21 Apr 2019 17:44:23 +0200 Subject: [PATCH] Use localtime_r instead of localtime This pleases linters that dislike localtime(3). --- configure.ac | 1 + src/ccache.c | 3 +++ src/ccache.h | 3 +++ src/hashutil.c | 9 +++++---- src/stats.c | 10 ++++++---- src/util.c | 19 +++++++++++++++---- 6 files changed, 33 insertions(+), 12 deletions(-) diff --git a/configure.ac b/configure.ac index 7df32ae66..78cc2a66f 100644 --- a/configure.ac +++ b/configure.ac @@ -86,6 +86,7 @@ AC_CHECK_FUNCS(gethostname) AC_CHECK_FUNCS(getopt_long) AC_CHECK_FUNCS(getpwuid) AC_CHECK_FUNCS(gettimeofday) +AC_CHECK_FUNCS(localtime_r) AC_CHECK_FUNCS(mkstemp) AC_CHECK_FUNCS(realpath) AC_CHECK_FUNCS(setenv) diff --git a/src/ccache.c b/src/ccache.c index c10c36ba2..b3f751c41 100644 --- a/src/ccache.c +++ b/src/ccache.c @@ -3688,6 +3688,9 @@ ccache(int argc, char *argv[]) set_up_signal_handlers(); #endif + // Needed for portability when using localtime_r. + tzset(); + orig_args = args_init(argc, argv); initialize(); diff --git a/src/ccache.h b/src/ccache.h index d9acf0310..d6bf18345 100644 --- a/src/ccache.h +++ b/src/ccache.h @@ -183,6 +183,9 @@ char *format_parsable_size_with_suffix(uint64_t size); bool parse_size_with_suffix(const char *str, uint64_t *size); char *x_realpath(const char *path); char *gnu_getcwd(void); +#ifndef HAVE_LOCALTIME_R +struct tm *localtime_r(const time_t *timep, struct tm *result); +#endif #ifndef HAVE_STRTOK_R char *strtok_r(char *str, const char *delim, char **saveptr); #endif diff --git a/src/hashutil.c b/src/hashutil.c index 789bf437b..289ba1717 100644 --- a/src/hashutil.c +++ b/src/hashutil.c @@ -107,12 +107,13 @@ hash_source_code_string( // Make sure that the hash sum changes if the (potential) expansion of // __DATE__ changes. time_t t = time(NULL); - struct tm *now = localtime(&t); + struct tm now; + localtime_r(&t, &now); cc_log("Found __DATE__ in %s", path); hash_delimiter(hash, "date"); - hash_int(hash, now->tm_year); - hash_int(hash, now->tm_mon); - hash_int(hash, now->tm_mday); + hash_int(hash, now.tm_year); + hash_int(hash, now.tm_mon); + hash_int(hash, now.tm_mday); } if (result & HASH_SOURCE_CODE_FOUND_TIME) { // We don't know for sure that the program actually uses the __TIME__ diff --git a/src/stats.c b/src/stats.c index 1e8e4970a..aadb2ac15 100644 --- a/src/stats.c +++ b/src/stats.c @@ -300,9 +300,10 @@ static char * format_timestamp(uint64_t timestamp) { if (timestamp > 0) { - struct tm *tm = localtime((time_t *)×tamp); + struct tm tm; + localtime_r((time_t *)×tamp, &tm); char buffer[100]; - strftime(buffer, sizeof(buffer), "%c", tm); + strftime(buffer, sizeof(buffer), "%c", &tm); return format(" %s", buffer); } else { return NULL; @@ -533,9 +534,10 @@ stats_summary(void) printf("secondary config (readonly) %s\n", secondary_config_path ? secondary_config_path : ""); if (last_updated > 0) { - struct tm *tm = localtime(&last_updated); + struct tm tm; + localtime_r(&last_updated, &tm); char timestamp[100]; - strftime(timestamp, sizeof(timestamp), "%c", tm); + strftime(timestamp, sizeof(timestamp), "%c", &tm); printf("stats updated %s\n", timestamp); } diff --git a/src/util.c b/src/util.c index 7fa9fe6d4..961d3393c 100644 --- a/src/util.c +++ b/src/util.c @@ -94,15 +94,15 @@ log_prefix(bool log_updated_time) #ifdef HAVE_GETTIMEOFDAY if (log_updated_time) { char timestamp[100]; - struct tm *tm; + struct tm tm; struct timeval tv; gettimeofday(&tv, NULL); #ifdef __MINGW64_VERSION_MAJOR - tm = localtime((time_t *)&tv.tv_sec); + localtime_r((time_t *)&tv.tv_sec, &tm); #else - tm = localtime(&tv.tv_sec); + localtime_r(&tv.tv_sec, &tm); #endif - strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", tm); + strftime(timestamp, sizeof(timestamp), "%Y-%m-%dT%H:%M:%S", &tm); snprintf(prefix, sizeof(prefix), "[%s.%06d %-5d] ", timestamp, (int)tv.tv_usec, (int)getpid()); } @@ -1223,6 +1223,17 @@ gnu_getcwd(void) } } +#ifndef HAVE_LOCALTIME_R +// localtime_r replacement. +struct tm * +localtime_r(const time_t *timep, struct tm *result) +{ + struct tm *tm = localtime(timep); + *result = *tm; + return result; +} +#endif + #ifndef HAVE_STRTOK_R // strtok_r replacement. char * -- 2.47.2