Skip to content

Commit 4755a34

Browse files
committed
Merge branch 'dd/time-reentrancy'
Avoid gmtime() and localtime() and prefer their reentrant counterparts. * dd/time-reentrancy: mingw: use {gm,local}time_s as backend for {gm,local}time_r archive-zip.c: switch to reentrant localtime_r date.c: switch to reentrant {gm,local}time_r
2 parents 37c2619 + 0109d67 commit 4755a34

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

archive-zip.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -603,18 +603,18 @@ static void write_zip_trailer(const struct object_id *oid)
603603
static void dos_time(timestamp_t *timestamp, int *dos_date, int *dos_time)
604604
{
605605
time_t time;
606-
struct tm *t;
606+
struct tm tm;
607607

608608
if (date_overflows(*timestamp))
609609
die(_("timestamp too large for this system: %"PRItime),
610610
*timestamp);
611611
time = (time_t)*timestamp;
612-
t = localtime(&time);
612+
localtime_r(&time, &tm);
613613
*timestamp = time;
614614

615-
*dos_date = t->tm_mday + (t->tm_mon + 1) * 32 +
616-
(t->tm_year + 1900 - 1980) * 512;
617-
*dos_time = t->tm_sec / 2 + t->tm_min * 32 + t->tm_hour * 2048;
615+
*dos_date = tm.tm_mday + (tm.tm_mon + 1) * 32 +
616+
(tm.tm_year + 1900 - 1980) * 512;
617+
*dos_time = tm.tm_sec / 2 + tm.tm_min * 32 + tm.tm_hour * 2048;
618618
}
619619

620620
static int archive_zip_config(const char *var, const char *value, void *data)

compat/mingw.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1018,16 +1018,16 @@ int pipe(int filedes[2])
10181018

10191019
struct tm *gmtime_r(const time_t *timep, struct tm *result)
10201020
{
1021-
/* gmtime() in MSVCRT.DLL is thread-safe, but not reentrant */
1022-
memcpy(result, gmtime(timep), sizeof(struct tm));
1023-
return result;
1021+
if (gmtime_s(result, timep) == 0)
1022+
return result;
1023+
return NULL;
10241024
}
10251025

10261026
struct tm *localtime_r(const time_t *timep, struct tm *result)
10271027
{
1028-
/* localtime() in MSVCRT.DLL is thread-safe, but not reentrant */
1029-
memcpy(result, localtime(timep), sizeof(struct tm));
1030-
return result;
1028+
if (localtime_s(result, timep) == 0)
1029+
return result;
1030+
return NULL;
10311031
}
10321032

10331033
char *mingw_getcwd(char *pointer, int len)

date.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -64,16 +64,16 @@ static time_t gm_time_t(timestamp_t time, int tz)
6464
* thing, which means that tz -0100 is passed in as the integer -100,
6565
* even though it means "sixty minutes off"
6666
*/
67-
static struct tm *time_to_tm(timestamp_t time, int tz)
67+
static struct tm *time_to_tm(timestamp_t time, int tz, struct tm *tm)
6868
{
6969
time_t t = gm_time_t(time, tz);
70-
return gmtime(&t);
70+
return gmtime_r(&t, tm);
7171
}
7272

73-
static struct tm *time_to_tm_local(timestamp_t time)
73+
static struct tm *time_to_tm_local(timestamp_t time, struct tm *tm)
7474
{
7575
time_t t = time;
76-
return localtime(&t);
76+
return localtime_r(&t, tm);
7777
}
7878

7979
/*
@@ -283,6 +283,7 @@ static void show_date_normal(struct strbuf *buf, timestamp_t time, struct tm *tm
283283
const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
284284
{
285285
struct tm *tm;
286+
struct tm tmbuf = { 0 };
286287
struct tm human_tm = { 0 };
287288
int human_tz = -1;
288289
static struct strbuf timebuf = STRBUF_INIT;
@@ -318,11 +319,11 @@ const char *show_date(timestamp_t time, int tz, const struct date_mode *mode)
318319
}
319320

320321
if (mode->local)
321-
tm = time_to_tm_local(time);
322+
tm = time_to_tm_local(time, &tmbuf);
322323
else
323-
tm = time_to_tm(time, tz);
324+
tm = time_to_tm(time, tz, &tmbuf);
324325
if (!tm) {
325-
tm = time_to_tm(0, 0);
326+
tm = time_to_tm(0, 0, &tmbuf);
326327
tz = 0;
327328
}
328329

@@ -959,10 +960,11 @@ void datestamp(struct strbuf *out)
959960
{
960961
time_t now;
961962
int offset;
963+
struct tm tm = { 0 };
962964

963965
time(&now);
964966

965-
offset = tm_to_time_t(localtime(&now)) - now;
967+
offset = tm_to_time_t(localtime_r(&now, &tm)) - now;
966968
offset /= 60;
967969

968970
date_string(now, offset, out);

0 commit comments

Comments
 (0)