From: Dag-Erling Smørgrav Date: Wed, 24 Jun 2026 15:47:08 +0000 (+0200) Subject: libarchive: Fix TIME_MAX and TIME_MIN X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=fc6bca4b060a3167103f8c33ff3ca8a3f1695866;p=thirdparty%2Flibarchive.git libarchive: Fix TIME_MAX and TIME_MIN --- diff --git a/libarchive/archive_integer.h b/libarchive/archive_integer.h index 6427c622c..1e7159279 100644 --- a/libarchive/archive_integer.h +++ b/libarchive/archive_integer.h @@ -49,6 +49,9 @@ #ifdef HAVE_STDINT_H #include #endif +#ifdef HAVE_TIME_H +#include +#endif #ifdef HAVE_UNISTD_H #include #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 diff --git a/libarchive/archive_read_support_format_mtree.c b/libarchive/archive_read_support_format_mtree.c index 66e2cb160..ce70da167 100644 --- a/libarchive/archive_read_support_format_mtree.c +++ b/libarchive/archive_read_support_format_mtree.c @@ -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); } diff --git a/libarchive/archive_read_support_format_tar.c b/libarchive/archive_read_support_format_tar.c index ac3dd446c..a5e3db2f3 100644 --- a/libarchive/archive_read_support_format_tar.c +++ b/libarchive/archive_read_support_format_tar.c @@ -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); }