-
Notifications
You must be signed in to change notification settings - Fork 37
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
71632a1
commit 548a355
Showing
2 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
# <expect-error> | ||
a = 1 / 0 | ||
|
||
# <expect-error> | ||
b = a / 0 | ||
|
||
# safe | ||
d = a / 2 | ||
|
||
# Safe | ||
puts a/2 | ||
|
||
# <expect-error> | ||
puts a/0 | ||
|
||
# <expect-error> c is being used as divisor | ||
c = 0 | ||
|
||
puts a/c | ||
|
||
d = a/c |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
language: ruby | ||
name: ruby_divide_by_zero | ||
message: "Avoid division by zero; it raises a ZeroDivisionError and can crash the application." | ||
category: error | ||
severity: error | ||
patterns: | ||
- > | ||
((binary | ||
left: (_) | ||
right: (integer) @denominator) @binary | ||
(#match? @binary "/") | ||
(#eq? @denominator "0")) @ruby_divide_by_zero | ||
- > | ||
((assignment | ||
left: (identifier) @var_name | ||
right: (integer) @zero_value) | ||
(#eq? @zero_value "0") | ||
(_) | ||
(assignment | ||
left: (identifier) | ||
right: (binary | ||
left: (identifier) | ||
right: (identifier) @divisor) @binary) | ||
(#eq? @var_name @divisor) | ||
(#match? @binary "/")) @ruby_divide_by_zero | ||
- > | ||
((assignment | ||
left: (identifier) @var_name | ||
right: (integer) @zero_value) | ||
(#eq? @zero_value "0") | ||
(_) | ||
(call | ||
method: (identifier) | ||
arguments: (argument_list | ||
(binary | ||
left: (identifier) | ||
right: (identifier) @divisor) @binary)) | ||
(#eq? @var_name @divisor) | ||
(#match? @binary "/")) @ruby_divide_by_zero | ||
exclude: | ||
- "test/**" | ||
- "*_test.rb" | ||
- "tests/**" | ||
- "__tests__/**" | ||
description: | | ||
Division by zero is undefined in mathematics and raises a `ZeroDivisionError` in Ruby, | ||
potentially causing application crashes or unexpected behavior. | ||
Why is this a problem? | ||
- Raises an unhandled exception, leading to program termination. | ||
- Can cause logic errors and disrupt application flow. | ||
- Indicates a lack of input validation or defensive programming. | ||
Remediation: | ||
- Ensure the divisor is not zero before performing division. | ||
- Use conditional checks or exception handling to prevent crashes. | ||
Example Fix: | ||
```ruby | ||
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" | ||
``` |