]> git.ipfire.org Git - thirdparty/libarchive.git/commitdiff
libarchive: Fix TIME_MAX and TIME_MIN
authorDag-Erling Smørgrav <des@des.dev>
Wed, 24 Jun 2026 15:47:08 +0000 (17:47 +0200)
committerDag-Erling Smørgrav <des@des.dev>
Wed, 24 Jun 2026 16:25:00 +0000 (18:25 +0200)
libarchive/archive_integer.h
libarchive/archive_read_support_format_mtree.c
libarchive/archive_read_support_format_tar.c

index 6427c622cd2982857e9b63afa2bca1046b62bbf9..1e715927901ecdd983f70d0248da13dd1224aaf8 100644 (file)
@@ -49,6 +49,9 @@
 #ifdef HAVE_STDINT_H
 #include <stdint.h>
 #endif
+#ifdef HAVE_TIME_H
+#include <time.h>
+#endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #endif
@@ -244,4 +247,21 @@ archive_ckd_sub_i64(int64_t *result, int64_t a, int64_t b)
 #endif
 }
 
+#if !defined(TIME_MAX)
+#define TIME_MAX (((time_t)0 < (time_t)-1) ? (time_t)~0 :              \
+           sizeof(time_t) == sizeof(long long) ? (time_t)LLONG_MAX :   \
+           sizeof(time_t) == sizeof(long) ? (time_t)LONG_MAX :         \
+           sizeof(time_t) == sizeof(int) ? (time_t)INT_MAX :           \
+           sizeof(time_t) == sizeof(short) ? (time_t)SHRT_MAX :        \
+           1 /* I give up */)
+#endif
+#if !defined(TIME_MIN)
+#define TIME_MIN (((time_t)0 < (time_t)-1) ? (time_t)0 :               \
+           sizeof(time_t) == sizeof(long long) ? (time_t)LLONG_MIN :   \
+           sizeof(time_t) == sizeof(long) ? (time_t)LONG_MIN :         \
+           sizeof(time_t) == sizeof(int) ? (time_t)INT_MIN :           \
+           sizeof(time_t) == sizeof(short) ? (time_t)SHRT_MIN :        \
+           -1 /* I give up */)
+#endif
+
 #endif
index 66e2cb1605322962b1468066169fec5d423e4938..ce70da1670e733cbe90630df95e35b52e958f0c6 100644 (file)
@@ -141,56 +141,6 @@ static int64_t     mtree_atol(char **, int base);
 static size_t  mtree_strnlen(const char *, size_t);
 #endif
 
-/*
- * There's no standard for TIME_T_MAX/TIME_T_MIN.  So we compute them
- * here.  TODO: Move this to configure time, but be careful
- * about cross-compile environments.
- */
-static int64_t
-get_time_t_max(void)
-{
-#if defined(TIME_T_MAX)
-       return TIME_T_MAX;
-#else
-       /* ISO C allows time_t to be a floating-point type,
-          but POSIX requires an integer type.  The following
-          should work on any system that follows the POSIX
-          conventions. */
-       if (((time_t)0) < ((time_t)-1)) {
-               /* Time_t is unsigned */
-               return (~(time_t)0);
-       } else {
-               /* Time_t is signed. */
-               /* Assume it's the same as int64_t or int32_t */
-               if (sizeof(time_t) == sizeof(int64_t)) {
-                       return (time_t)INT64_MAX;
-               } else {
-                       return (time_t)INT32_MAX;
-               }
-       }
-#endif
-}
-
-static int64_t
-get_time_t_min(void)
-{
-#if defined(TIME_T_MIN)
-       return TIME_T_MIN;
-#else
-       if (((time_t)0) < ((time_t)-1)) {
-               /* Time_t is unsigned */
-               return (time_t)0;
-       } else {
-               /* Time_t is signed. */
-               if (sizeof(time_t) == sizeof(int64_t)) {
-                       return (time_t)INT64_MIN;
-               } else {
-                       return (time_t)INT32_MIN;
-               }
-       }
-#endif
-}
-
 #ifdef HAVE_STRNLEN
 #define mtree_strnlen(a,b) strnlen(a,b)
 #else
@@ -1781,8 +1731,6 @@ parse_keyword(struct archive_read *a, struct mtree *mtree,
                }
                if (strcmp(key, "time") == 0) {
                        int64_t m;
-                       int64_t my_time_t_max = get_time_t_max();
-                       int64_t my_time_t_min = get_time_t_min();
                        long ns = 0;
 
                        *parsed_kws |= MTREE_HAS_MTIME;
@@ -1802,10 +1750,10 @@ parse_keyword(struct archive_read *a, struct mtree *mtree,
                                else
                                        ns = (long)v;
                        }
-                       if (m > my_time_t_max)
-                               m = my_time_t_max;
-                       else if (m < my_time_t_min)
-                               m = my_time_t_min;
+                       if (m > TIME_MAX)
+                               m = TIME_MAX;
+                       else if (m < TIME_MIN)
+                               m = TIME_MIN;
                        archive_entry_set_mtime(entry, (time_t)m, ns);
                        return (ARCHIVE_OK);
                }
index ac3dd446cb684432962922bf899306a806f9669c..a5e3db2f363ce28e5044f73d0c6706639ae23df4 100644 (file)
@@ -251,36 +251,6 @@ static const size_t fflags_limit = 512; /* Longest fflags */
 static const size_t acl_limit = 131072; /* Longest textual ACL: 128kiB */
 static const int64_t entry_limit = 0xfffffffffffffffLL; /* 2^60 bytes = 1 ExbiByte */
 
-/*
- * There's no standard for TIME_T_MAX.  So we compute it
- * here.  TODO: Move this to configure time, but be careful
- * about cross-compile environments.
- */
-static time_t
-get_time_t_max(void)
-{
-#if defined(TIME_T_MAX)
-        return TIME_T_MAX;
-#else
-        /* ISO C allows time_t to be a floating-point type,
-           but POSIX requires an integer type.  The following
-           should work on any system that follows the POSIX
-           conventions. */
-        if (((time_t)0) < ((time_t)-1)) {
-                /* Time_t is unsigned */
-                return (~(time_t)0);
-        } else {
-                /* Time_t is signed. */
-                /* Assume it's the same as int64_t or int32_t */
-                if (sizeof(time_t) == sizeof(int64_t)) {
-                        return (time_t)INT64_MAX;
-                } else {
-                        return (time_t)INT32_MAX;
-                }
-        }
-#endif
-}
-
 int
 archive_read_support_format_gnutar(struct archive *a)
 {
@@ -1402,7 +1372,7 @@ header_common(struct archive_read *a, struct tar *tar,
                int64_t t64 = tar_atol(header->mtime, sizeof(header->mtime));
                time_t t = (time_t)t64;
                if ((int64_t)t != t64) { /* time_t overflowed */
-                       t = get_time_t_max();
+                       t = TIME_MAX;
                }
                archive_entry_set_mtime(entry, t, 0);
        }