Clamp the calculated ETA to within the size of a long integer to avoid over/underflows, and round off the floating point value rather than just truncating the decimal portion.

This commit is contained in:
Andrew Wood
2026-04-19 17:57:32 +01:00
parent c19d16b3c8
commit b2f44b6724
11 changed files with 55 additions and 33 deletions
+20 -1
View File
@@ -17,6 +17,7 @@
#include <errno.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#ifdef HAVE_TERMIOS_H
#include <termios.h>
@@ -28,6 +29,10 @@
#endif
#endif
#if HAVE_MATH_H
#include <math.h>
#endif
/*
* If ncurses is unavailable, and USE_POPEN_TPUTS is defined, then
* popen("tputs") if used to find out whether colours are supported.
@@ -247,9 +252,23 @@ long pv_seconds_remaining(const off_t so_far, const off_t total, const long doub
amount_left = (long double) (total - so_far) / rate;
/* TODO: check whether rounding would be better here. */
/* Clamp the amount to a value that fits inside a long integer. */
#ifdef LONG_MAX
if (amount_left > (long double) LONG_MAX)
amount_left = (long double) LONG_MAX;
#endif
#ifdef LONG_MIN
if (amount_left < (long double) LONG_MIN)
amount_left = (long double) LONG_MIN;
#endif
#if HAVE_LROUNDL
/*@-unrecog@ */
return lroundl(amount_left);
/*@+unrecog@ *//* splint doesn't know of lroundl(). */
#else
return (long) amount_left;
#endif
}
/*