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 --- cpio/test/test_option_t.c | 24 ++++++++++++++++++++-- .../test/test_write_format_zip_compression_store.c | 23 +++++++++++++++++++-- libarchive/test/test_write_format_zip_file.c | 21 ++++++++++++++++++- libarchive/test/test_write_format_zip_file_zip64.c | 21 ++++++++++++++++++- test_utils/test_main.c | 23 +++++++++++++++++++-- 5 files changed, 104 insertions(+), 8 deletions(-) diff --git a/cpio/test/test_option_t.c b/cpio/test/test_option_t.c index eaa73fa3..0f2dda27 100644 --- a/cpio/test/test_option_t.c +++ b/cpio/test/test_option_t.c @@ -36,6 +36,14 @@ DEFINE_TEST(test_option_t) time_t mtime; char date[32]; char date2[32]; + 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 /* List reference archive, make sure the TOC is correct. */ extract_reference_file("test_option_t.cpio"); @@ -87,11 +95,23 @@ DEFINE_TEST(test_option_t) #ifdef HAVE_LOCALE_H setlocale(LC_ALL, ""); #endif +#if defined(HAVE_LOCALTIME_R) + tmptr = localtime_r(&mtime, &tmbuf); +#elif defined(HAVE__LOCALTIME64_S) + tmptime = mtime; + terr = _localtime64_s(&tmbuf, &tmptime); + if (terr) + tmptr = NULL; + else + tmptr = &tmbuf; +#else + tmptr = localtime(&mtime); +#endif #if defined(_WIN32) && !defined(__CYGWIN__) - strftime(date2, sizeof(date2)-1, "%b %d %Y", localtime(&mtime)); + strftime(date2, sizeof(date2)-1, "%b %d %Y", tmptr); _snprintf(date, sizeof(date)-1, "%12.12s file", date2); #else - strftime(date2, sizeof(date2)-1, "%b %e %Y", localtime(&mtime)); + strftime(date2, sizeof(date2)-1, "%b %e %Y", tmptr); snprintf(date, sizeof(date)-1, "%12.12s file", date2); #endif assertEqualMem(p + 42, date, strlen(date)); diff --git a/libarchive/test/test_write_format_zip_compression_store.c b/libarchive/test/test_write_format_zip_compression_store.c index c969a41d..ed090878 100644 --- a/libarchive/test/test_write_format_zip_compression_store.c +++ b/libarchive/test/test_write_format_zip_compression_store.c @@ -128,12 +128,31 @@ static void verify_uncompressed_contents(const char *buff, size_t used) /* Misc variables */ unsigned long crc; - struct tm *tm = localtime(&now); - + struct tm *tm; +#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 /* p is the pointer to walk over the central directory, * q walks over the local headers, the data and the data descriptors. */ const char *p, *q, *local_header, *extra_start; +#if defined(HAVE_LOCALTIME_R) + tm = localtime_r(&now, &tmbuf); +#elif defined(HAVE__LOCALTIME64_S) + tmptime = now; + terr = _localtime64_s(&tmbuf, &tmptime); + if (terr) + tm = NULL; + else + tm = &tmbuf; +#else + tm = localtime(&now); +#endif + /* Remember the end of the archive in memory. */ buffend = buff + used; diff --git a/libarchive/test/test_write_format_zip_file.c b/libarchive/test/test_write_format_zip_file.c index 2868123b..7796a48c 100644 --- a/libarchive/test/test_write_format_zip_file.c +++ b/libarchive/test/test_write_format_zip_file.c @@ -73,7 +73,14 @@ DEFINE_TEST(test_write_format_zip_file) struct archive *a; struct archive_entry *ae; time_t t = 1234567890; - struct tm *tm = localtime(&t); + struct tm *tm; +#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 size_t used, buffsize = 1000000; unsigned long crc; int file_perm = 00644; @@ -91,6 +98,18 @@ DEFINE_TEST(test_write_format_zip_file) zip_compression = 0; #endif +#if defined(HAVE_LOCALTIME_R) + tm = localtime_r(&t, &tmbuf); +#elif defined(HAVE__LOCALTIME64_S) + tmptime = t; + terr = _localtime64_s(&tmbuf, &tmptime); + if (terr) + tm = NULL; + else + tm = &tmbuf; +#else + tm = localtime(&t); +#endif buff = malloc(buffsize); /* Create a new archive in memory. */ diff --git a/libarchive/test/test_write_format_zip_file_zip64.c b/libarchive/test/test_write_format_zip_file_zip64.c index 71da9866..c4161bc3 100644 --- a/libarchive/test/test_write_format_zip_file_zip64.c +++ b/libarchive/test/test_write_format_zip_file_zip64.c @@ -75,7 +75,14 @@ DEFINE_TEST(test_write_format_zip_file_zip64) struct archive *a; struct archive_entry *ae; time_t t = 1234567890; - struct tm *tm = localtime(&t); + struct tm *tm; +#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 size_t used, buffsize = 1000000; unsigned long crc; int file_perm = 00644; @@ -92,6 +99,18 @@ DEFINE_TEST(test_write_format_zip_file_zip64) zip_compression = 0; #endif +#if defined(HAVE_LOCALTIME_R) + tm = localtime_r(&t, &tmbuf); +#elif defined(HAVE__LOCALTIME64_S) + tmptime = t; + terr = _localtime64_s(&tmbuf, &tmptime); + if (terr) + tm = NULL; + else + tm = &tmbuf; +#else + tm = localtime(&t); +#endif buff = malloc(buffsize); /* Create a new archive in memory. */ 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