]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
tests: silence localtime() CodeQL warnings
authorMartin Matuska <martin@matuska.de>
Wed, 7 Dec 2022 23:20:58 +0000 (00:20 +0100)
committerMartin Matuska <martin@matuska.de>
Wed, 7 Dec 2022 23:24:33 +0000 (00:24 +0100)
Use localtime_r() or _localtime64_s() if available

cpio/test/test_option_t.c
libarchive/test/test_write_format_zip_compression_store.c
libarchive/test/test_write_format_zip_file.c
libarchive/test/test_write_format_zip_file_zip64.c
test_utils/test_main.c

index eaa73fa3a01647ba2627aaab852793e8d8442e9c..0f2dda27cc20a114bdf59c952c8d6036f4b33f10 100644 (file)
@@ -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));
index c969a41d4d41c94da4e7a57679c0fd50f92e7efe..ed0908787579502b760407f2bc1f14982cc1a4ae 100644 (file)
@@ -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;
 
index 2868123b08b9b14ea7018cfcbe912a3a1b94c413..7796a48cdac30f4ba0765b4a3aacb0b6f65602bd 100644 (file)
@@ -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. */
index 71da98668d8db36d9b7a57f207201874c348501d..c4161bc3a89b9c9126f04fd5c3b630fd4a5e460f 100644 (file)
@@ -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. */
index fd5c6da7928064d6826d336fbcebb015c1e34b1a..f5f5a713be3c97e5ef71a117c03cba4845b3a9ab 100644 (file)
@@ -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))) {