Uploaded image for project: 'Rules Repository'
  1. Rules Repository
  2. RSPEC-3518

Zero should not be a possible denominator

    Details

    • 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, Objective-C, PHP, PL/SQL, Swift, VB.Net
    • Covered Languages:
      C, C++, Java
    • 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 would result 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

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                freddy.mallet Freddy Mallet (Inactive)
              • Votes:
                0 Vote for this issue
                Watchers:
                2 Start watching this issue

                Dates

                • Created:
                  Updated: