From 0d7cc9a041461ba03de90476db2c9bcb0a391112 Mon Sep 17 00:00:00 2001 From: Martin Matuska Date: Thu, 8 Dec 2022 00:20:58 +0100 Subject: tests: silence localtime() CodeQL warnings Use localtime_r() or _localtime64_s() if available --- test_utils/test_main.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) (limited to 'test_utils') diff --git a/test_utils/test_main.c b/test_utils/test_main.c index fd5c6da7..f5f5a713 100644 --- a/test_utils/test_main.c +++ b/test_utils/test_main.c @@ -3869,6 +3869,14 @@ main(int argc, char **argv) int tmp2_len; #endif time_t now; + struct tm *tmptr; +#if defined(HAVE_LOCALTIME_R) || defined(HAVE__LOCALTIME64_S) + struct tm tmbuf; +#endif +#if defined(HAVE__LOCALTIME64_S) + errno_t terr; + __time64_t tmptime; +#endif char *refdir_alloc = NULL; const char *progname; char **saved_argv; @@ -4103,9 +4111,20 @@ main(int argc, char **argv) */ now = time(NULL); for (i = 0; ; i++) { +#if defined(HAVE_LOCALTIME_R) + tmptr = localtime_r(&now, &tmbuf); +#elif defined(HAVE__LOCALTIME64_S) + tmptime = now; + terr = _localtime64_s(&tmbuf, &tmptime); + if (terr) + tmptr = NULL; + else + tmptr = &tmbuf; +#else + tmptr = localtime(&now); +#endif strftime(tmpdir_timestamp, sizeof(tmpdir_timestamp), - "%Y-%m-%dT%H.%M.%S", - localtime(&now)); + "%Y-%m-%dT%H.%M.%S", tmptr); if ((strlen(tmp) + 1 + strlen(progname) + 1 + strlen(tmpdir_timestamp) + 1 + 3) > (sizeof(tmpdir) / sizeof(char))) { -- cgit v1.2.1