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/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 +++++++++++++++++++- 3 files changed, 61 insertions(+), 4 deletions(-) (limited to 'libarchive') 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. */ -- cgit v1.2.1