Create rule S3518: Zero should not be a possible denominator (#1772)
Co-authored-by: chrislain-razafimahefa-sonarsource <chrislain-razafimahefa-sonarsource@users.noreply.github.com>
This commit is contained in:
parent
94f3f6fb43
commit
a2cb22285b
16
rules/S3518/python/metadata.json
Normal file
16
rules/S3518/python/metadata.json
Normal file
@ -0,0 +1,16 @@
|
||||
{
|
||||
"tags": [
|
||||
"cwe",
|
||||
"denial-of-service",
|
||||
"cert"
|
||||
],
|
||||
"securityStandards": {
|
||||
"CERT": [
|
||||
"NUM02-J.",
|
||||
"INT33-C."
|
||||
],
|
||||
"CWE": [
|
||||
369
|
||||
]
|
||||
}
|
||||
}
|
59
rules/S3518/python/rule.adoc
Normal file
59
rules/S3518/python/rule.adoc
Normal file
@ -0,0 +1,59 @@
|
||||
If the denominator to a division or modulo operation is zero it would result in a fatal error.
|
||||
|
||||
== Why is this an issue?
|
||||
|
||||
This is an issue because dividing by zero is a forbidden operation which leads to a fatal error.
|
||||
|
||||
=== What is the potential impact?
|
||||
|
||||
This issue can lead your program to an unexpected halt with all the inconveniences it entails.
|
||||
|
||||
== How to fix it
|
||||
|
||||
Make sure that zero never reaches the denominator.
|
||||
|
||||
=== Code examples
|
||||
|
||||
==== Noncompliant code example
|
||||
|
||||
[source,text,diff-id=1,diff-type=noncompliant]
|
||||
----
|
||||
def non_compliant():
|
||||
z = 0
|
||||
if (unknown()):
|
||||
# ...
|
||||
z = 4
|
||||
else:
|
||||
# ...
|
||||
z = 1 / z
|
||||
----
|
||||
|
||||
==== Compliant solution
|
||||
|
||||
[source,text,diff-id=1,diff-type=compliant]
|
||||
----
|
||||
def compliant():
|
||||
z = 0
|
||||
if (unknown()):
|
||||
# ...
|
||||
z = 4
|
||||
else:
|
||||
# ...
|
||||
z = 1
|
||||
z = 1 / z
|
||||
----
|
||||
|
||||
=== How does this work?
|
||||
|
||||
By ensuring that for all the paths that can define the variable ++z++, when none assigns it zero, we are sure that the issue is fixed.
|
||||
|
||||
//=== Pitfalls
|
||||
|
||||
//=== Going the extra mile
|
||||
|
||||
|
||||
//== Resources
|
||||
//=== Documentation
|
||||
//=== Articles & blog posts
|
||||
//=== Conference presentations
|
||||
//=== Standards
|
Loading…
x
Reference in New Issue
Block a user