schweikert / fping

High performance ping tool
https://fping.org
Other
1.01k stars 250 forks source link

Defensive coding: guard against a division by zero #293

Closed auerswal closed 7 months ago

auerswal commented 8 months ago

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.

coveralls commented 8 months ago

Coverage Status

coverage: 82.435% (-0.2%) from 82.67% when pulling c9dfe977e2d550bbe420cdbe04849b1143646a8b on auerswal:no_div_by_zero into 2a609b856d1aa796690d13ff4e9e1433334e1301 on schweikert:develop.

auerswal commented 8 months ago

The added check against division by zero also adds a new code path, but I do not know how to trigger the potential division by zero. Thus the drop in code coverage by tests is kind of expected. I'd like to ask for merging this pull request anyway. Thanks!

auerswal commented 7 months ago

Thanks!