]> git.ipfire.org Git - thirdparty/ccache.git/commitdiff
Use localtime_r instead of localtime
authorJoel Rosdahl <joel@rosdahl.net>
Sun, 21 Apr 2019 15:44:23 +0000 (17:44 +0200)
committerJoel Rosdahl <joel@rosdahl.net>
Sun, 21 Apr 2019 18:45:45 +0000 (20:45 +0200)
This pleases linters that dislike localtime(3).

configure.ac
src/ccache.c
src/ccache.h
src/hashutil.c
src/stats.c
src/util.c

index 7df32ae6649f812f78abc6093bf1ff51e30cf08b..78cc2a66f7dd035842e1045619edf148d0bd64f8 100644 (file)
@@ -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)
index c10c36ba25061289c13679cf872ccde2062530ad..b3f751c4156876714b2e589f330251938ec7291e 100644 (file)
@@ -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();
index d9acf03105428f02c27629c68a94cd4b7b44a995..d6bf183456480b6f79203a88a2313afbeebb7c11 100644 (file)
@@ -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
index 789bf437b51efcffe869a5eb67e89f540ba9aa7e..289ba1717e23372fea291cf105612854e5af116c 100644 (file)
@@ -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__
index 1e8e4970a21a8770a67b70662ee1834983095544..aadb2ac158308ba38cd59a1074a4dd015cc3bc6f 100644 (file)
@@ -300,9 +300,10 @@ static char *
 format_timestamp(uint64_t timestamp)
 {
        if (timestamp > 0) {
-               struct tm *tm = localtime((time_t *)&timestamp);
+               struct tm tm;
+               localtime_r((time_t *)&timestamp, &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);
        }
 
index 7fa9fe6d4e81fb234381a1817438f637677f6ea0..961d3393ce0f1e3cd7a1564083aac0851ef56222 100644 (file)
@@ -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 *