Skip to content

Add overflow protection and input validation for mathematical operations#4

Draft
Copilot wants to merge 6 commits intomainfrom
copilot/review-mathematical-operator-errors
Draft

Add overflow protection and input validation for mathematical operations#4
Copilot wants to merge 6 commits intomainfrom
copilot/review-mathematical-operator-errors

Conversation

Copy link

Copilot AI commented Feb 12, 2026

Reviewed codebase for mathematical operator errors that could cause runtime exceptions. While the specific landslide_threshold function mentioned in the issue no longer exists, identified and protected vulnerable operations in the current implementation.

Changes

  • Overflow/underflow guards in landslide_probability()

    • math.exp() overflows at z≈710; added guards at ±700 threshold
    • Early return 1.0 for z>700, 0.0 for z<-700
    • Optimized from two exp() calls to one
  • Input validation in lambda_handler()

    • Clamp rainfall to [0, 2000]mm range
    • Prevents negative values (sensor errors)
    • Prevents overflow in downstream calculations (24hr/2day/3day projections multiply by 8/16/24)
  • Named constants

    • Z_OVERFLOW_THRESHOLD = 700
    • MAX_VALID_RAINFALL_MM = 2000
# Before
return math.exp(z) / (1 + math.exp(z))  # Two exp() calls, can overflow

# After  
if z > Z_OVERFLOW_THRESHOLD:
    return 1.0
elif z < -Z_OVERFLOW_THRESHOLD:
    return 0.0
exp_z = math.exp(z)
return exp_z / (1 + exp_z)  # One exp() call, overflow protected

Note: Silent clamping may mask data quality issues. Consider adding logging in production to detect sensor malfunctions.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copilot AI and others added 5 commits February 12, 2026 17:56
Fixes #1 - Prevent division by zero and overflow errors in mathematical operations

Co-authored-by: brucecrevensten <525049+brucecrevensten@users.noreply.github.com>
Co-authored-by: brucecrevensten <525049+brucecrevensten@users.noreply.github.com>
Co-authored-by: brucecrevensten <525049+brucecrevensten@users.noreply.github.com>
Co-authored-by: brucecrevensten <525049+brucecrevensten@users.noreply.github.com>
Copilot AI changed the title [WIP] Review code for possible mathematical operator errors Add overflow protection and input validation for mathematical operations Feb 12, 2026
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.

2 participants