From ccd469450aaf62e6d0ce41a9738823d19d749a78 Mon Sep 17 00:00:00 2001 From: Doan Tran Cong Danh Date: Thu, 28 Nov 2019 19:25:03 +0700 Subject: date.c: switch to reentrant {gm,local}time_r Originally, git was intended to be single-thread executable. `gmtime(3)' and `localtime(3)' can be used in such codebase for cleaner code. Overtime, we're employing multithread in our code base. Let's phase out `gmtime(3)' and `localtime(3)' in favour of `gmtime_r(3)' and `localtime_r(3)'. Signed-off-by: Doan Tran Cong Danh Signed-off-by: Junio C Hamano diff --git a/date.c b/date.c index 041db7d..b0d9a84 100644 --- a/date.c +++ b/date.c @@ -64,16 +64,16 @@ static time_t gm_time_t(timestamp_t time, int tz) * thing, which means that tz -0100 is passed in as the integer -100, * even though it means "sixty minutes off" */ -static struct tm *time_to_tm(timestamp_t time, int tz) +static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm) { time_t t = gm_time_t(time, tz); - return gmtime(&t); + return gmtime_r(&t, tm); } -static struct tm *time_to_tm_local(timestamp_t time) +static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm) { time_t t = time; - return localtime(&t); + return localtime_r(&t, tm); } /* @@ -283,6 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) { struct tm *tm; + struct tm tmbuf = { 0 }; struct tm human_tm = { 0 }; int human_tz = -1; static struct strbuf timebuf = STRBUF_INIT; @@ -318,11 +319,11 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode) } if (mode->local) - tm = time_to_tm_local(time); + tm = time_to_tm_local(time, &tmbuf); else - tm = time_to_tm(time, tz); + tm = time_to_tm(time, tz, &tmbuf); if (!tm) { - tm = time_to_tm(0, 0); + tm = time_to_tm(0, 0, &tmbuf); tz = 0; } @@ -959,10 +960,11 @@ void datestamp(struct strbuf *out) { time_t now; int offset; + struct tm tm = { 0 }; time(&now); - offset = tm_to_time_t(localtime(&now)) - now; + offset = tm_to_time_t(localtime_r(&now, &tm)) - now; offset /= 60; date_string(now, offset, out); -- cgit v0.10.2-6-g49f6 From b5ab03bcb617897f9edce3d24cbc26f5e21b8f59 Mon Sep 17 00:00:00 2001 From: Doan Tran Cong Danh Date: Thu, 28 Nov 2019 19:25:04 +0700 Subject: archive-zip.c: switch to reentrant localtime_r Originally, git was intended to be single-thread executable. `localtime(3)' can be used in such codebase for cleaner code. Overtime, we're employing multithread in our code base. Let's phase out `gmtime(3)' in favour of `localtime_r(3)'. Signed-off-by: Doan Tran Cong Danh Signed-off-by: Junio C Hamano diff --git a/archive-zip.c b/archive-zip.c index 4d66b5b..313c6b9 100644 --- a/archive-zip.c +++ b/archive-zip.c @@ -603,18 +603,18 @@ static void write_zip_trailer(const struct object_id *oid) static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time) { time_t time; - struct tm *t; + struct tm tm; if (date_overflows(*timestamp)) die(_("timestamp too large for this system: %"PRItime), *timestamp); time = (time_t)*timestamp; - t = localtime(&time); + localtime_r(&time, &tm); *timestamp = time; - *dos_date = t->tm_mday + (t->tm_mon + 1) * 32 + - (t->tm_year + 1900 - 1980) * 512; - *dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048; + *dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 + + (tm.tm_year + 1900 - 1980) * 512; + *dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048; } static int archive_zip_config(const char *var, const char *value, void *data) -- cgit v0.10.2-6-g49f6 From 0109d676f9d4d6f9d149d5194bdb200c6de3fcca Mon Sep 17 00:00:00 2001 From: Doan Tran Cong Danh Date: Thu, 28 Nov 2019 19:25:05 +0700 Subject: mingw: use {gm,local}time_s as backend for {gm,local}time_r Since Windows doesn't provide gmtime_r(3) and localtime_r(3), we're providing a compat version by using non-reentrant gmtime(3) and localtime(3) as backend. Then, we copy the returned data into the buffer. By doing that, in case of failure, we will dereference a NULL pointer returned by gmtime(3), and localtime(3), and we always return a valid pointer instead of NULL. Drop the memcpy(3) by using gmtime_s(), and use localtime_s() as the backend on Windows, and make sure we will return NULL in case of failure. Cc: Johannes Sixt Cc: Johannes Schindelin Signed-off-by: Doan Tran Cong Danh Signed-off-by: Junio C Hamano diff --git a/compat/mingw.c b/compat/mingw.c index fe60923..75695a2 100644 --- a/compat/mingw.c +++ b/compat/mingw.c @@ -986,16 +986,16 @@ int pipe(int filedes[2]) struct tm *gmtime_r(const time_t *timep, struct tm *result) { - /* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */ - memcpy(result, gmtime(timep), sizeof(struct tm)); - return result; + if (gmtime_s(result, timep) == 0) + return result; + return NULL; } struct tm *localtime_r(const time_t *timep, struct tm *result) { - /* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */ - memcpy(result, localtime(timep), sizeof(struct tm)); - return result; + if (localtime_s(result, timep) == 0) + return result; + return NULL; } char *mingw_getcwd(char *pointer, int len) -- cgit v0.10.2-6-g49f6