Skip to content

Commit 4337343

Browse files
keszybzpoettering
authored andcommitted
Do not divide by zero when displaying percentages
The total number can be zero even if we did some operation, and then we'd divide by zero, crashing immediately with FPE.
1 parent 3eb5fcd commit 4337343

File tree

1 file changed

+9
-6
lines changed

1 file changed

+9
-6
lines changed

src/casync-tool.c

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,25 +1058,28 @@ static int verbose_print_done_extract(CaSync *s) {
10581058

10591059
if (n_local_requests != UINT64_MAX)
10601060
log_info("Chunk requests fulfilled from local store: %" PRIu64 " (%" PRIu64 "%%)",
1061-
n_local_requests, n_local_requests * 100U / total_requests);
1061+
n_local_requests,
1062+
total_requests > 0 ? n_local_requests * 100U / total_requests : 0);
10621063
if (n_local_bytes != UINT64_MAX)
10631064
log_info("Bytes used from local store: %s (%" PRIu64 "%%)",
10641065
format_bytes(buffer, sizeof(buffer), n_local_bytes),
1065-
n_local_bytes * 100U / total_bytes);
1066+
total_bytes > 0 ? n_local_bytes * 100U / total_bytes : 0);
10661067
if (n_seed_requests != UINT64_MAX)
10671068
log_info("Chunk requests fulfilled from local seed: %" PRIu64 " (%" PRIu64 "%%)",
1068-
n_seed_requests, n_seed_requests * 100U / total_requests);
1069+
n_seed_requests,
1070+
total_requests > 0 ? n_seed_requests * 100U / total_requests : 0);
10691071
if (n_seed_bytes != UINT64_MAX)
10701072
log_info("Bytes used from local seed: %s (%" PRIu64 "%%)",
10711073
format_bytes(buffer, sizeof(buffer), n_seed_bytes),
1072-
n_seed_bytes * 100U / total_bytes);
1074+
total_bytes > 0 ? n_seed_bytes * 100U / total_bytes : 0);
10731075
if (n_remote_requests != UINT64_MAX)
10741076
log_info("Chunk requests fulfilled from remote store: %" PRIu64 " (%" PRIu64 "%%)",
1075-
n_remote_requests, n_remote_requests * 100U / total_requests);
1077+
n_remote_requests,
1078+
total_requests > 0 ? n_remote_requests * 100U / total_requests : 0);
10761079
if (n_remote_bytes != UINT64_MAX)
10771080
log_info("Bytes used from remote store: %s (%" PRIu64 "%%)",
10781081
format_bytes(buffer, sizeof(buffer), n_remote_bytes),
1079-
n_remote_bytes * 100U / total_bytes);
1082+
total_bytes > 0 ? n_remote_bytes * 100U / total_bytes : 0);
10801083

10811084
return 1;
10821085
}

0 commit comments

Comments
 (0)