]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
Use localtime_r and gmtime_r if supported
authorMartin Matuska <martin@matuska.org>
Mon, 13 Jan 2020 11:37:32 +0000 (12:37 +0100)
committerMartin Matuska <martin@matuska.org>
Mon, 13 Jan 2020 12:39:58 +0000 (13:39 +0100)
Found by LGTM.com code analysis

cpio/cpio.c
libarchive/archive_getdate.c
libarchive/archive_read_support_format_rar.c
libarchive/archive_write_set_format_zip.c
tar/util.c

index 4fd394dea5f1659de49a46a543fc6ee020c5680e..f8dd62c54cbef00fc79d7af1febd1469e87bbf7e 100644 (file)
@@ -1139,6 +1139,10 @@ list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
        const char              *fmt;
        time_t                   mtime;
        static time_t            now;
+       struct tm               *ltime;
+#ifdef HAVE_LOCALTIME_R
+       struct tm               tmbuf;
+#endif
 
        if (!now)
                time(&now);
@@ -1186,7 +1190,12 @@ list_item_verbose(struct cpio *cpio, struct archive_entry *entry)
        else
                fmt = cpio->day_first ? "%e %b %H:%M" : "%b %e %H:%M";
 #endif
-       strftime(date, sizeof(date), fmt, localtime(&mtime));
+#ifdef HAVE_LOCALTIME_R
+       ltime = localtime_r(&mtime, &tmbuf);
+#else
+       ltime = localtime(&mtime)
+#endif
+       strftime(date, sizeof(date), fmt, ltime);
 
        fprintf(out, "%s%3d %-8s %-8s %8s %12s %s",
            archive_entry_strmode(entry),
index 030c083ce716b6c623c64d067a716eebbfddfd71..aff4ac4c96df6efd1ad9a8b24f69e7879b6c70d6 100644 (file)
@@ -27,6 +27,7 @@
 **  This code is in the public domain and has no copyright.
 */
 
+#include "archive_platform.h"
 #ifdef __FreeBSD__
 #include <sys/cdefs.h>
 __FBSDID("$FreeBSD$");
@@ -694,8 +695,12 @@ Convert(time_t Month, time_t Day, time_t Year,
        signed char DaysInMonth[12] = {
                31, 0, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31
        };
-       time_t  Julian;
-       int     i;
+       time_t          Julian;
+       int             i;
+       struct tm       *ltime;
+#ifdef HAVE_LOCALTIME_R
+       struct tm       tmbuf;
+#endif
 
        if (Year < 69)
                Year += 2000;
@@ -722,21 +727,39 @@ Convert(time_t Month, time_t Day, time_t Year,
        Julian *= DAY;
        Julian += Timezone;
        Julian += Hours * HOUR + Minutes * MINUTE + Seconds;
+#ifdef HAVE_LOCALTIME_R
+       ltime = localtime_r(&Julian, &tmbuf);
+#else
+       ltime = localtime(&Julian);
+#endif
        if (DSTmode == DSTon
-           || (DSTmode == DSTmaybe && localtime(&Julian)->tm_isdst))
+           || (DSTmode == DSTmaybe && ltime->tm_isdst))
                Julian -= HOUR;
        return Julian;
 }
 
-
 static time_t
 DSTcorrect(time_t Start, time_t Future)
 {
-       time_t  StartDay;
-       time_t  FutureDay;
+       time_t          StartDay;
+       time_t          FutureDay;
+       struct tm       *ltime;
+#ifdef HAVE_LOCALTIME_R
+       struct tm       tmbuf;
+#endif
 
-       StartDay = (localtime(&Start)->tm_hour + 1) % 24;
-       FutureDay = (localtime(&Future)->tm_hour + 1) % 24;
+#ifdef HAVE_LOCALTIME_R
+       ltime = localtime_r(&Start, &tmbuf);
+#else
+       ltime = localtime(&Start);
+#endif
+       StartDay = (ltime->tm_hour + 1) % 24;
+#ifdef HAVE_LOCALTIME_R
+       ltime = localtime_r(&Future, &tmbuf);
+#else
+       ltime = localtime(&Future);
+#endif
+       FutureDay = (ltime->tm_hour + 1) % 24;
        return (Future - Start) + (StartDay - FutureDay) * HOUR;
 }
 
@@ -747,9 +770,16 @@ RelativeDate(time_t Start, time_t zone, int dstmode,
 {
        struct tm       *tm;
        time_t  t, now;
+#ifdef HAVE_GMTIME_R
+       struct tm       tmbuf;
+#endif
 
        t = Start - zone;
+#ifdef HAVE_GMTIME_R
+       tm = gmtime_r(&t, &tmbuf);
+#else
        tm = gmtime(&t);
+#endif
        now = Start;
        now += DAY * ((DayNumber - tm->tm_wday + 7) % 7);
        now += 7 * DAY * (DayOrdinal <= 0 ? DayOrdinal : DayOrdinal - 1);
@@ -765,10 +795,17 @@ RelativeMonth(time_t Start, time_t Timezone, time_t RelMonth)
        struct tm       *tm;
        time_t  Month;
        time_t  Year;
+#ifdef HAVE_LOCALTIME_R
+       struct tm       tmbuf;
+#endif
 
        if (RelMonth == 0)
                return 0;
+#ifdef HAVE_LOCALTIME_R
+       tm = localtime_r(&Start, &tmbuf);
+#else
        tm = localtime(&Start);
+#endif
        Month = 12 * (tm->tm_year + 1900) + tm->tm_mon + RelMonth;
        Year = Month / 12;
        Month = Month % 12 + 1;
@@ -913,20 +950,30 @@ __archive_get_date(time_t now, const char *p)
        gds = &_gds;
 
        /* Look up the current time. */
+#ifdef HAVE_LOCALTIME_R
+       tm = localtime_r(&now, &local);
+#else
        memset(&local, 0, sizeof(local));
-       tm = localtime (&now);
+       tm = localtime(&now);
+#endif
        if (tm == NULL)
                return -1;
+#ifndef HAVE_LOCALTIME_R
        local = *tm;
+#endif
 
        /* Look up UTC if we can and use that to determine the current
         * timezone offset. */
+#ifdef HAVE_GMTIME_R
+       gmt_ptr = gmtime_r(&now, &gmt);
+#else
        memset(&gmt, 0, sizeof(gmt));
-       gmt_ptr = gmtime (&now);
+       gmt_ptr = gmtime(&now);
        if (gmt_ptr != NULL) {
                /* Copy, in case localtime and gmtime use the same buffer. */
                gmt = *gmt_ptr;
        }
+#endif
        if (gmt_ptr != NULL)
                tzone = difftm (&gmt, &local);
        else
@@ -960,7 +1007,11 @@ __archive_get_date(time_t now, const char *p)
         * time components instead of the local timezone. */
        if (gds->HaveZone && gmt_ptr != NULL) {
                now -= gds->Timezone;
-               gmt_ptr = gmtime (&now);
+#ifdef HAVE_GMTIME_R
+               gmt_ptr = gmtime_r(&now, &gmt);
+#else
+               gmt_ptr = gmtime(&now);
+#endif
                if (gmt_ptr != NULL)
                        local = *gmt_ptr;
                now += gds->Timezone;
index 41e5a3cadd90c8bc8b3faf79dfbbabca63c5b74e..153dc908d265c0991fb985d3be636e0bba41cd0c 100644 (file)
@@ -1720,6 +1720,9 @@ read_exttime(const char *p, struct rar *rar, const char *endp)
   unsigned rmode, flags, rem, j, count;
   int ttime, i;
   struct tm *tm;
+#ifdef HAVE_LOCALTIME_R
+  struct tm tmbuf;
+#endif
   time_t t;
   long nsec;
 
@@ -1753,7 +1756,11 @@ read_exttime(const char *p, struct rar *rar, const char *endp)
         rem = (((unsigned)(unsigned char)*p) << 16) | (rem >> 8);
         p++;
       }
+#ifdef HAVE_LOCALTIME_R
+      tm = localtime_r(&t, &tmbuf);
+#else
       tm = localtime(&t);
+#endif
       nsec = tm->tm_sec + rem / NS_UNIT;
       if (rmode & 4)
       {
index f28a8c3a341f33c14d2389a3371c01734d05d5d1..1a020aadd8b9b5e895da90b8a81f7610446aa42e 100644 (file)
@@ -1372,10 +1372,17 @@ dos_time(const time_t unix_time)
 {
        struct tm *t;
        unsigned int dt;
+#ifdef HAVE_LOCALTIME_R
+       struct tm tmbuf;
+#endif
 
        /* This will not preserve time when creating/extracting the archive
         * on two systems with different time zones. */
+#ifdef HAVE_LOCALTIME_R
+       t = localtime_r(&unix_time, &tmbuf);
+#else
        t = localtime(&unix_time);
+#endif
 
        /* MSDOS-style date/time is only between 1980-01-01 and 2107-12-31 */
        if (t->tm_year < 1980 - 1900)
index 662db5baa79664d54f4ef27faa57334ad7d73430..85c5446a9059d9a4705adaa38404c7653da44e0f 100644 (file)
@@ -666,6 +666,10 @@ list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
        const char              *fmt;
        time_t                   tim;
        static time_t            now;
+       struct tm               *ltime;
+#ifdef HAVE_LOCALTIME_R
+       struct tm               tmbuf;
+#endif
 
        /*
         * We avoid collecting the entire list in memory at once by
@@ -737,7 +741,12 @@ list_item_verbose(struct bsdtar *bsdtar, FILE *out, struct archive_entry *entry)
                fmt = bsdtar->day_first ? DAY_FMT " %b  %Y" : "%b " DAY_FMT "  %Y";
        else
                fmt = bsdtar->day_first ? DAY_FMT " %b %H:%M" : "%b " DAY_FMT " %H:%M";
-       strftime(tmp, sizeof(tmp), fmt, localtime(&tim));
+#ifdef HAVE_LOCALTIME_R
+       ltime = localtime_r(&tim, &tmbuf);
+#else
+       ltime = localtime(&tim);
+#endif
+       strftime(tmp, sizeof(tmp), fmt, ltime);
        fprintf(out, " %s ", tmp);
        safe_fprintf(out, "%s", archive_entry_pathname(entry));