From b2f44b6724c9390a2fd312b64c4cfa997bcf3d7a Mon Sep 17 00:00:00 2001 From: Andrew Wood Date: Sun, 19 Apr 2026 17:57:32 +0100 Subject: [PATCH] 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. --- configure.ac | 2 ++ docs/NEWS.md | 1 + po/cs.po | 8 ++++---- po/de.po | 8 ++++---- po/fi.po | 8 ++++---- po/fr.po | 8 ++++---- po/pl.po | 8 ++++---- po/pt.po | 8 ++++---- po/ru.po | 8 ++++---- po/tr.po | 8 ++++---- src/pv/display.c | 21 ++++++++++++++++++++- 11 files changed, 55 insertions(+), 33 deletions(-) diff --git a/configure.ac b/configure.ac index 503e841..7ffe7d8 100644 --- a/configure.ac +++ b/configure.ac @@ -48,9 +48,11 @@ AC_CHECK_DECLS([SA_SIGINFO], [], [], [[#include ]]) AC_SEARCH_LIBS([sqrtl],[m]) AC_SEARCH_LIBS([fmod],[m]) +AC_SEARCH_LIBS([lroundl],[m]) AC_CHECK_HEADERS([math.h]) AC_CHECK_FUNCS([sqrtl]) AC_CHECK_FUNCS([fmod]) +AC_CHECK_FUNCS([lroundl]) dnl Libraries that may contain key functions. AC_SEARCH_LIBS([clock_gettime],[rt]) diff --git a/docs/NEWS.md b/docs/NEWS.md index 6aa91c8..0386790 100644 --- a/docs/NEWS.md +++ b/docs/NEWS.md @@ -10,6 +10,7 @@ * *cleanup:* fix warning in pv_remote_check() stub on OpenBSD * *cleanup:* use dynamic line buffers for **--watchfd** process lookups * *cleanup:* replace the blanket 100,000-hour clamp on displayed times with a million days for timer and ETA, and 7,000 years for estimated finish time, on 64-bit systems + * *cleanup:* use rounding when calculating ETA, and avoid integer overflows, particularly on 32-bit systems * *cleanup:* standardise the error reporting mechanism * *cleanup:* improve code comments diff --git a/po/cs.po b/po/cs.po index 9dcf1ca..ed215d7 100644 --- a/po/cs.po +++ b/po/cs.po @@ -1,7 +1,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2024-10-13 20:43+0000\n" "Last-Translator: mmatous \n" "Language-Team: Czech \n" @@ -455,15 +455,15 @@ msgstr "selhal pokus o uzamčení" msgid "failed to open terminal" msgstr "selhalo otevření terminálu" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "yzafpnum kMGTPEZY" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "yzafpnum KMGTPEZY" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "selhala alokace vyrovnávací paměti" diff --git a/po/de.po b/po/de.po index 795e134..60464a4 100644 --- a/po/de.po +++ b/po/de.po @@ -1,7 +1,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2025-01-14 20:39+0000\n" "Last-Translator: Benny \n" "Language-Team: German \n" @@ -478,15 +478,15 @@ msgstr "Sperrversuch fehlgeschlagen" msgid "failed to open terminal" msgstr "konnte Terminal nicht öffnen" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "yzafpnµm kMGTPEZY" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "yzafpnµm KMGTPEZY" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "Speicherzuweisung für den Puffer fehlgeschlagen" diff --git a/po/fi.po b/po/fi.po index 17be46e..ad1c233 100644 --- a/po/fi.po +++ b/po/fi.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: pv 1.9.34\n" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2026-03-24 23:25+0000\n" "Last-Translator: Ricky-Tigg \n" "Language-Team: Finnish \n" @@ -460,15 +460,15 @@ msgstr "lukitusyritys epäonnistui" msgid "failed to open terminal" msgstr "päätelaitteen avaaminen epäonnistui" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "puskurin allokointi epäonnistui" diff --git a/po/fr.po b/po/fr.po index b784319..c64da51 100644 --- a/po/fr.po +++ b/po/fr.po @@ -1,7 +1,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2025-05-31 08:58+0000\n" "Last-Translator: milimarg \n" "Language-Team: French \n" @@ -482,15 +482,15 @@ msgstr "échec de la tentative de verrouillage" msgid "failed to open terminal" msgstr "échec de l'ouverture du terminal" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "yzafpnum kMGTPEZY" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "yzafpnum KMGTPEZY" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "échec de l'allocation de mémoire tampon" diff --git a/po/pl.po b/po/pl.po index 05244d6..2fffe6e 100644 --- a/po/pl.po +++ b/po/pl.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2026-04-06 15:34+0000\n" "Last-Translator: Coral Pink \n" "Language-Team: Polish \n" @@ -475,15 +475,15 @@ msgstr "blokada zasobów nie powiodła się" msgid "failed to open terminal" msgstr "nie udało się otworzyć terminala" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "yzafpnum kMGTPEZY" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "yzafpnum KMGTPEZY" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "nie udało się zaalokować bufora" diff --git a/po/pt.po b/po/pt.po index 4f0845d..0e2fb98 100644 --- a/po/pt.po +++ b/po/pt.po @@ -6,7 +6,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -461,15 +461,15 @@ msgstr "tentativa de bloqueio falhou" msgid "failed to open terminal" msgstr "erro abrindo o terminal" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "erro alocando o buffer" diff --git a/po/ru.po b/po/ru.po index d6927fe..9409386 100644 --- a/po/ru.po +++ b/po/ru.po @@ -1,7 +1,7 @@ msgid "" msgstr "" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2025-01-15 19:08+0000\n" "Last-Translator: 0ko <0ko@users.noreply.translate.codeberg.org>\n" "Language-Team: Russian \n" @@ -456,15 +456,15 @@ msgstr "попытка блокировки не удалась" msgid "failed to open terminal" msgstr "не удалось открыть терминал" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "не удалось выделить память для буфера" diff --git a/po/tr.po b/po/tr.po index 68eab17..56ced42 100644 --- a/po/tr.po +++ b/po/tr.po @@ -2,7 +2,7 @@ msgid "" msgstr "" "Project-Id-Version: pv 1.8.14\n" "Report-Msgid-Bugs-To: https://codeberg.org/ivarch/pv/issues\n" -"POT-Creation-Date: 2026-04-19 16:49+0100\n" +"POT-Creation-Date: 2026-04-19 17:22+0100\n" "PO-Revision-Date: 2025-10-05 20:09+0000\n" "Last-Translator: Oğuz Ersen \n" "Language-Team: Turkish \n" @@ -457,15 +457,15 @@ msgstr "kilit denemesi başarısız oldu" msgid "failed to open terminal" msgstr "terminal açılamadı" -#: src/pv/display.c:292 +#: src/pv/display.c:311 msgid "yzafpnum kMGTPEZY" msgstr "yzafpnum kMGTPEZY" -#: src/pv/display.c:310 +#: src/pv/display.c:329 msgid "yzafpnum KMGTPEZY" msgstr "yzafpnum KMGTPEZY" -#: src/pv/display.c:1063 src/pv/state.c:879 src/pv/transfer.c:1148 +#: src/pv/display.c:1082 src/pv/state.c:879 src/pv/transfer.c:1148 msgid "buffer allocation failed" msgstr "arabellek tahsisi başarısız oldu" diff --git a/src/pv/display.c b/src/pv/display.c index eb32219..2165b6f 100644 --- a/src/pv/display.c +++ b/src/pv/display.c @@ -17,6 +17,7 @@ #include #include #include +#include #ifdef HAVE_TERMIOS_H #include @@ -28,6 +29,10 @@ #endif #endif +#if HAVE_MATH_H +#include +#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 } /*