Skip to content

Validate divisor parameters in carb_math to prevent ZeroDivisionError#30

Open
CharlesCNorton wants to merge 1 commit into
tidepool-org:mainfrom
CharlesCNorton:guard-carb-math-divisors
Open

Validate divisor parameters in carb_math to prevent ZeroDivisionError#30
CharlesCNorton wants to merge 1 commit into
tidepool-org:mainfrom
CharlesCNorton:guard-carb-math-divisors

Conversation

@CharlesCNorton

Copy link
Copy Markdown

Summary

Two functions in carb_math.py divide by a parameter without validating it:

  • carb_glucose_effect: insulin_sensitivity / carb_ratio
  • parabolic_percent_absorption_at_time: 2 / pow(absorption_time, 2) and 4 / absorption_time

A zero (or negative) carb_ratio or absorption_time therefore raises an opaque
ZeroDivisionError from inside the carb-absorption model. Because these values come from
carb-ratio and absorption-time settings, a single bad entry crashes the glucose-effect
calculation instead of surfacing the misconfiguration.

For example:

parabolic_absorbed_carbs(total=0, time=0, absorption_time=0)   # ZeroDivisionError
carb_glucose_effect(start, 10, at, carb_ratio=0, ...)          # ZeroDivisionError

Change

Both quantities are strictly positive by definition (grams per unit, and minutes), so this
validates them and raises a clear ValueError rather than dividing. Silently substituting a
value 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.py suite passes (13 tests).

Found by static analysis.

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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant