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

Calculations should not overflow

    XMLWordPrintable

    Details

    • Message:
      Hide
      * This calculation is guaranteed to overflow the maximum value of [type].
      * This calculation is likely to overflow the maximum value of [type].
      Show
      * This calculation is guaranteed to overflow the maximum value of [type]. * This calculation is likely to overflow the maximum value of [type].
    • Highlighting:
      • primary: the entire calculation
      • secondary: the type into which the overflowing value is expected to fit: variable, return type (in the case of return x*y, ...
    • Default Severity:
      Major
    • Impact:
      Low
    • Likelihood:
      High
    • Default Quality Profiles:
      Sonar way
    • Targeted languages:
      Java, Swift, VB.Net
    • Covered Languages:
      C#, C, C++, Objective-C
    • Remediation Function:
      Constant/Issue
    • Constant Cost:
      5min
    • Analysis Scope:
      Main Sources, Test Sources
    • CERT:
      INT30-C.
    • MISRA C 2004:
      12.11
    • MISRA C 2012:
      12.4
    • FxCop:
      CA2233, OperationsShouldNotOverflow

      Description

      Numbers are infinite, but the types that hold them are not. Each numeric type has hard upper and lower bounds. Try to calculate or assign numbers beyond those bounds, and the result will be a value that has silently wrapped around from the expected positive value to a negative one, or vice versa.

      Noncompliant Code Example

      public int getTheNumber(int positiveInt) {
        int num = Integer.MAX_VALUE;
        return num + positiveInt;  // Noncompliant
      }
      

        Attachments

          Issue Links

            Activity

              People

              Assignee:
              Unassigned Unassigned
              Reporter:
              ann.campbell.2 Ann Campbell
              Votes:
              1 Vote for this issue
              Watchers:
              2 Start watching this issue

                Dates

                Created:
                Updated: