Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Defensive coding: guard against a division by zero #293

Merged
merged 1 commit into from
Feb 4, 2024

Conversation

auerswal
Copy link
Collaborator

@auerswal auerswal commented Jan 9, 2024

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.

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
Copy link

Coverage Status

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

@auerswal
Copy link
Collaborator Author

auerswal commented Jan 9, 2024

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!

@schweikert schweikert merged commit 290d944 into schweikert:develop Feb 4, 2024
6 of 9 checks passed
@auerswal
Copy link
Collaborator Author

auerswal commented Feb 4, 2024

Thanks!

@auerswal auerswal deleted the no_div_by_zero branch February 4, 2024 11:26
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants