diff --git a/src/pv/format/eta.c b/src/pv/format/eta.c index 36f996e..a12b1eb 100644 --- a/src/pv/format/eta.c +++ b/src/pv/format/eta.c @@ -10,6 +10,8 @@ #include "pv.h" #include "pv-internal.h" +#include + /* * Estimated time until completion. @@ -17,7 +19,7 @@ pvdisplay_bytecount_t pv_formatter_eta(pvformatter_args_t args) { char content[128]; /* flawfinder: ignore - bounded in pv_snprintf(). */ - long eta; + long eta, max_days; content[0] = '\0'; @@ -39,8 +41,15 @@ pvdisplay_bytecount_t pv_formatter_eta(pvformatter_args_t args) eta = 0; /* The ETA must be under 1,000,000 days so it's not too wide. */ - if ((eta / 86400L) >= 1000000L) - eta = 1000000L * 86400L - 1L; + /* If a "long" is 32 bits, only 24,855 days will fit in it. */ + max_days = 24855L; +#ifdef LONG_MAX + if (LONG_MAX > 2147483647L) + max_days = 1000000; +#endif + + if ((eta / 86400L) >= max_days) + eta = max_days * 86400L - 1L; /* * If the ETA is more than a day, include a day count as well as diff --git a/src/pv/format/fineta.c b/src/pv/format/fineta.c index c587b46..ccf9b47 100644 --- a/src/pv/format/fineta.c +++ b/src/pv/format/fineta.c @@ -13,6 +13,7 @@ #include #include +#include /* @@ -23,7 +24,7 @@ pvdisplay_bytecount_t pv_formatter_fineta(pvformatter_args_t args) char content[128]; /* flawfinder: ignore - bounded by strftime(). */ time_t now, then; struct tm *time_ptr; - long eta; + long eta, max_years; const char *time_format; bool show_fineta; @@ -55,8 +56,15 @@ pvdisplay_bytecount_t pv_formatter_fineta(pvformatter_args_t args) eta = 0; /* Clamp the ETA to 7,000 years max so the date isn't too wide. */ - if ((eta / 31536000L) > 7000L) - eta = 31536000L * 7000L; + /* If a "long" is 32 bits, only 68 years will fit in it. */ + max_years = 68; +#ifdef LONG_MAX + if (LONG_MAX > 2147483647L) + max_years = 7000; +#endif + + if ((eta / 31536000L) > max_years) + eta = 31536000L * max_years; /* * Only include the date if the ETA is more than 6 hours