Skip to content

Commit

Permalink
Defensive coding: guard against a division by zero
Browse files Browse the repository at this point in the history
In the function print_per_system_stats(), if packets have been
lost, the number of sent packets is checked to be positive before
dividing by it.  If no packets have been lost, this is not checked.
Either the existing check is not needed, or both code paths need
the check.

The function print_per_system_splits() is quite similar to
print_per_system_stats(), and has the equivalent guards against a
division by zero in both code paths, not just one of them.

In the spirit of defensive coding, I think it is better to be safe
and add the missing guard against a division by zero.
  • Loading branch information
auerswal committed Jan 9, 2024
1 parent 2a609b8 commit c9dfe97
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion src/fping.c
Original file line number Diff line number Diff line change
Expand Up @@ -1655,7 +1655,7 @@ void print_per_system_stats(void)
else {
fprintf(stderr, " xmt/rcv/%%return = %d/%d/%d%%",
h->num_sent, h->num_recv,
((h->num_recv * 100) / h->num_sent));
h->num_sent > 0 ? ((h->num_recv * 100) / h->num_sent) : 0);
}

if (h->num_recv) {
Expand Down

0 comments on commit c9dfe97

Please # to comment.