Validate divisor parameters in carb_math to prevent ZeroDivisionError#30
Open
CharlesCNorton wants to merge 1 commit into
Open
Validate divisor parameters in carb_math to prevent ZeroDivisionError#30CharlesCNorton wants to merge 1 commit into
CharlesCNorton wants to merge 1 commit into
Conversation
carb_glucose_effect divides by carb_ratio and parabolic_percent_absorption_at_time divides by absorption_time without checking them, so a zero or negative value raised an opaque ZeroDivisionError from inside the carb-absorption model instead of rejecting the input. Both are strictly positive physical quantities, so validate them and raise a clear ValueError. Adds tests for the rejection and for the unaffected positive path.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two functions in
carb_math.pydivide by a parameter without validating it:carb_glucose_effect:insulin_sensitivity / carb_ratioparabolic_percent_absorption_at_time:2 / pow(absorption_time, 2)and4 / absorption_timeA zero (or negative)
carb_ratioorabsorption_timetherefore raises an opaqueZeroDivisionErrorfrom inside the carb-absorption model. Because these values come fromcarb-ratio and absorption-time settings, a single bad entry crashes the glucose-effect
calculation instead of surfacing the misconfiguration.
For example:
Change
Both quantities are strictly positive by definition (grams per unit, and minutes), so this
validates them and raises a clear
ValueErrorrather than dividing. Silently substituting avalue was avoided on purpose: in dosing math an incorrect carb effect is worse than an explicit
rejection, so the functions fail loudly on invalid input.
Tests
Adds tests that the guards reject non-positive inputs and that the positive path is unchanged.
The full
tests/test_carb_math.pysuite passes (13 tests).Found by static analysis.