Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

checker: ruby_divide_by_zero #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

Thiru-moorthi
Copy link
Contributor

@Thiru-moorthi Thiru-moorthi commented Feb 24, 2025

Description

This PR introduces a Ruby checker that detects potential division by zero errors, which can cause runtime exceptions and application crashes.

Detection Logic

The checker flags the following patterns:

  • Direct division by zero (e.g., 10 / 0).
  • Variables assigned to zero and later used as divisors.
  • Division by variables known to be zero through prior assignments.

Why is this a problem?

  • Unhandled Exceptions: Raises a ZeroDivisionError, leading to crashes if not handled.
  • Logic Flaws: May cause unpredictable behavior or incorrect results.
  • Stability Risks: Lack of validation reflects poor error handling.

Recommended Remediation

Prevent division by zero with proper checks:

  • Validate inputs before performing division.
  • Use conditional checks or handle exceptions.

Example Fix

def safe_divide(a, b)
  return "Division by zero is not allowed" if b.zero?
  a / b
end

puts safe_divide(10, 2)  # => 5
puts safe_divide(10, 0)  # => "Division by zero is not allowed"

Exclusions

The checker excludes test files to minimize false positives:
test/**,*_test.rb,tests/**,__tests__/**

References

Ruby Zero Division Error

Copy link

vercel bot commented Feb 24, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
globstar ⬜️ Ignored (Inspect) Visit Preview Feb 25, 2025 10:19am

Comment on lines +19 to +21
puts a/c

d = a/c
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removing line 21 (d = a/c) does not raise the issue in line 17, even though the issue still exists in line 19.

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