Skip to content

Commit

Permalink
Fix integer overflow when computing the progress percentage
Browse files Browse the repository at this point in the history
  • Loading branch information
amadvance committed Jan 3, 2024
1 parent a327006 commit e345a4b
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions cmdline/state.c
Original file line number Diff line number Diff line change
Expand Up @@ -4228,6 +4228,11 @@ int state_progress_begin(struct snapraid_state* state, block_off_t blockstart, b
return 1;
}

static unsigned umuldiv(unsigned v, unsigned mul, unsigned div)
{
return (uint32_t)((uint64_t)v * mul / div);
}

void state_progress_end(struct snapraid_state* state, block_off_t countpos, block_off_t countmax, data_off_t countsize)
{
if (state->opt.gui) {
Expand All @@ -4245,7 +4250,7 @@ void state_progress_end(struct snapraid_state* state, block_off_t countpos, bloc

elapsed = now - state->progress_whole_start - state->progress_wasted;

msg_bar("%u%% completed, %u MB accessed", countpos * 100 / countmax, countsize_MB);
msg_bar("%u%% completed, %u MB accessed", umuldiv(countpos, 100, countmax), countsize_MB);

msg_bar(" in %u:%02u", (unsigned)(elapsed / 3600), (unsigned)((elapsed % 3600) / 60));

Expand Down Expand Up @@ -4499,7 +4504,7 @@ int state_progress(struct snapraid_state* state, struct snapraid_io* io, block_o

/* completion percentage */
if (countmax)
out_perc = countpos * 100 / countmax;
out_perc = umuldiv(countpos, 100, countmax);

/* if we have at least 5 measures */
if (state->progress_tick >= 5
Expand Down Expand Up @@ -4569,7 +4574,7 @@ int state_progress(struct snapraid_state* state, struct snapraid_io* io, block_o

/* estimate the remaining time in minutes */
if (delta_pos != 0)
out_eta = (countmax - countpos) * delta_time / (60 * delta_pos);
out_eta = umuldiv(countmax - countpos, delta_time, 60 * delta_pos);

if (state->opt.force_stats) {
os_clear();
Expand Down

0 comments on commit e345a4b

Please sign in to comment.