Skip to content

Commit bfffcac

Browse files
committed
clar(clar_summary_testsuite): avoid thread-unsafe localtime()
The `localtime()` function is inherently thread-unsafe and should not be used anymore. Let's use `localtime_r()` instead. Signed-off-by: Johannes Schindelin <[email protected]>
1 parent 7cb8b3c commit bfffcac

File tree

2 files changed

+5
-2
lines changed

2 files changed

+5
-2
lines changed

t/unit-tests/clar/clar.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@
7979
# else
8080
# define p_snprintf snprintf
8181
# endif
82+
83+
# define localtime_r(timer, buf) (localtime_s(buf, timer) == 0 ? buf : NULL)
8284
#else
8385
# include <sys/wait.h> /* waitpid(2) */
8486
# include <unistd.h>

t/unit-tests/clar/clar/summary.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@ static int clar_summary_testsuite(struct clar_summary *summary,
2323
int idn, const char *name, time_t timestamp,
2424
int test_count, int fail_count, int error_count)
2525
{
26-
struct tm *tm = localtime(&timestamp);
26+
struct tm tm;
2727
char iso_dt[20];
2828

29-
if (strftime(iso_dt, sizeof(iso_dt), "%Y-%m-%dT%H:%M:%S", tm) == 0)
29+
localtime_r(&timestamp, &tm);
30+
if (strftime(iso_dt, sizeof(iso_dt), "%Y-%m-%dT%H:%M:%S", &tm) == 0)
3031
return -1;
3132

3233
return fprintf(summary->fp, "\t<testsuite"

0 commit comments

Comments
 (0)