From 8a6817ad3ef1af4c7bf8f58932e73e78bdb8fc6c Mon Sep 17 00:00:00 2001 From: Andreas Engel Date: Sat, 21 Sep 2024 20:26:24 +0200 Subject: [PATCH] fix: incorrect progress percentage calculation Correct the wrong progress percentage calculation. This was visible especially when the blocksize was (much) larger than the actual file size, resulting in values greater than 100%. --- tftpd.hpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/tftpd.hpp b/tftpd.hpp index aad0ac7..e4cf1ab 100644 --- a/tftpd.hpp +++ b/tftpd.hpp @@ -327,8 +327,9 @@ class receiver : public server if (dp_->th_opcode == DATA) { // NOLINTNEXTLINE(cppcoreguidelines-pro-type-union-access) if (dp_->th_block == block) { - if (g_tsize != 0) { // NOTE: prevent division by zero! CK - size_t const percent = 100UL * (block * g_segsize) / g_tsize; + if (g_tsize != 0 && g_segsize != 0) { // NOTE: prevent division by zero! CK + auto totalBlocks = g_tsize / g_segsize + ((g_tsize % g_segsize) > 0 ? 1 : 0); + size_t const percent = (100UL * block) / totalBlocks; if (percent != percent_) { syslog(LOG_NOTICE, "tftpd: Progress: %lu%% received\n", percent); percent_ = percent;