summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Matuska <martin@matuska.org>2020-01-13 12:37:32 +0100
committerMartin Matuska <martin@matuska.org>2020-01-13 13:39:58 +0100
commit5be1a96f75a7ffc46cba8c63a09a193152338366 (patch)
tree99f2f4552728289ccd0b6b6391c122c38d3df173
parent2aaf7c5e88d495a0705e965519bfe9096765df6d (diff)
downloadlibarchive-5be1a96f75a7ffc46cba8c63a09a193152338366.tar.gz
Use localtime_r and gmtime_r if supported
Found by LGTM.com code analysis
-rw-r--r--cpio/cpio.c11
-rw-r--r--libarchive/archive_getdate.c73
-rw-r--r--libarchive/archive_read_support_format_rar.c7
-rw-r--r--libarchive/archive_write_set_format_zip.c7
-rw-r--r--tar/util.c11
5 files changed, 96 insertions, 13 deletions
diff --git a/cpio/cpio.c b/cpio/cpio.c
index 4fd394de..f8dd62c5 100644
--- a/cpio/cpio.c
+++ b/cpio/cpio.c
@@ -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),
diff --git a/libarchive/archive_getdate.c b/libarchive/archive_getdate.c
index 030c083c..aff4ac4c 100644
--- a/libarchive/archive_getdate.c
+++ b/libarchive/archive_getdate.c
@@ -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;
diff --git a/libarchive/archive_read_support_format_rar.c b/libarchive/archive_read_support_format_rar.c
index 41e5a3ca..153dc908 100644
--- a/libarchive/archive_read_support_format_rar.c
+++ b/libarchive/archive_read_support_format_rar.c
@@ -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)
{
diff --git a/libarchive/archive_write_set_format_zip.c b/libarchive/archive_write_set_format_zip.c
index f28a8c3a..1a020aad 100644
--- a/libarchive/archive_write_set_format_zip.c
+++ b/libarchive/archive_write_set_format_zip.c
@@ -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)
diff --git a/tar/util.c b/tar/util.c
index 662db5ba..85c5446a 100644
--- a/tar/util.c
+++ b/tar/util.c
@@ -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));