Skip to content

Commit

Permalink
fix: incorrect progress percentage calculation
Browse files Browse the repository at this point in the history
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%.
  • Loading branch information
Andreas Engel committed Sep 21, 2024
1 parent 04f0b34 commit 978d283
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions tftpd.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 978d283

Please sign in to comment.