# Zero should not be a possible denominator

XMLWordPrintable

#### Details

• Type: Bug Detection
• Status: Active
• Resolution: Unresolved
• Labels:
• Message:
Make sure 'xxxxx' can't be zero before doing this [division|modulation].
• Highlighting:
Hide
• primary: The denominator
• secondary: where the denominator is set to 0
• tertiary: where the denom is conditionally reset > 0
• message: Happens conditionally
• secondary: where the denominator is received as a parameter
• message: Could be 0
Show
primary: The denominator secondary: where the denominator is set to 0 tertiary: where the denom is conditionally reset > 0 message: Happens conditionally secondary: where the denominator is received as a parameter message: Could be 0
• Default Severity:
Critical
• Impact:
High
• Likelihood:
Low
• Default Quality Profiles:
Sonar way
• Targeted languages:
C#, Flex, JavaScript, PHP, PL/SQL, Swift, VB.Net
• Covered Languages:
C, C++, Java, Objective-C
• Remediation Function:
Constant/Issue
• Constant Cost:
5 min
• Analysis Scope:
Main Sources, Test Sources
• Implementation details:
• CERT:
NUM02-J., INT33-C.
• CWE:
CWE-369
• CPPCheck:
zerodiv

#### Description

If the denominator to a division or modulo operation is zero it results in a fatal error.

## Noncompliant Code Example

```void test_divide() {
int z = 0;
if (unknown()) {
// ..
z = 3;
} else {
// ..
}
z = 1 / z; // Noncompliant, possible division by zero
}
```

## Compliant Solution

```void test_divide() {
int z = 0;
if (unknown()) {
// ..
z = 3;
} else {
// ..
z = 1;
}
z = 1 / z;
}
```

## See

• MITRE, CWE-369 - Divide by zero
• CERT, NUM02-J. - Ensure that division and remainder operations do not result in divide-by-zero errors
• CERT, INT33-C. - Ensure that division and remainder operations do not result in divide-by-zero errors

#### Attachments

Progress

 1 C-Family: Zero should not be a possible denominator RSPEC-3594 Active Unassigned 2 Java RSPEC-5938 Active Unassigned

#### People

Assignee:
Unassigned
Reporter:
Freddy Mallet (Inactive)