Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
V3157. Suspicious division. Absolute va…
menu mobile close menu
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
Problems related to code analyzer
Additional information
toggle menu Contents

V3157. Suspicious division. Absolute value of the left operand is less than the right operand.

Apr 09 2020

The analyzer has detected one of the two types of integer operations – either a division or modulo operation – in which the absolute value of the left operand is always less than the absolute value of the right operand.

Such operations will return the following results:

  • division will always return 0;
  • modulo operation will always return the left operand.

Such an expression is very likely to contain an error or is simply redundant.

Consider the following contrived example:

public void Method()
{
  int a = 10;
  int b = 20;
  var c = a / b;
  ....
}

In this snippet, the 'a / b' expression will always evaluate to 0 since 'a < b'. To turn this expression into a real division operation, we need to cast the type of the 'a' variable to 'double':

public void Method()
{
  int a = 10;
  int b = 20;
  var c = (double)a / b;
  ....
}

The following example is taken from a real program:

public override Shipper CreateInstance(int i)
{
  ....
  return new Shipper 
  {
    ....
    DateCreated = new DateTime(i + 1 % 3000, // <=
                               (i % 11) + 1, 
                               (i % 27) + 1, 
                               0, 
                               0, 
                               0, 
                               DateTimeKind.Utc),
    ....
  };
}

The error here has to do with the wrong assumption about operation precedence. In the 'i + 1 % 3000' expression, the '1 % 3000' part will be evaluated first, thus always returning 1. Therefore, the value of the 'i' variable will always be added to 1. This is one way to fix this bug:

public override Shipper CreateInstance(int i)
{
  ....
  return new Shipper 
  {
    ....
    DateCreated = new DateTime((i + 1) % 3000, // <=
                               (i % 11) + 1, 
                               (i % 27) + 1, 
                               0, 
                               0, 
                               0, 
                               DateTimeKind.Utc),
    ....
  };
}

Here is another real-life example:

private void ValidateMultiRecords(StorageEnvironment env, 
                                  IEnumerable<string> trees, 
                                  int documentCount, 
                                  int i)
{
  for (var j = 0; j < 10; j++)
  {
    foreach (var treeName in trees)
    {
      var tree = tx.CreateTree(treeName);
      using (var iterator = tree.MultiRead((j % 10).ToString())) // <=
      {
        ....
      }
    }
  }
}

In this snippet, the 'j' variable is incremented over the range [0..9]. Therefore, the result of the 'j % 10' expression will always be equal to the value of 'j'. This is what the simpler correct version may look like:

private void ValidateMultiRecords(StorageEnvironment env, 
                                  IEnumerable<string> trees, 
                                  int documentCount, 
                                  int i)
{
  for (var j = 0; j < 10; j++)
  {
    foreach (var treeName in trees)
    {
      var tree = tx.CreateTree(treeName);
      using (var iterator = tree.MultiRead(j.ToString())) // <=
      {
        ....
      }
    }
  }
}

This diagnostic is classified as:

You can look at examples of errors detected by the V3157 diagnostic.

close form

Fill out the form in 2 simple steps below:

Your contact information:

Step 1
Congratulations! This is your promo code!

Desired license type:

Step 2
Team license
Enterprise license
** By clicking this button you agree to our Privacy Policy statement
close form
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

close form
Free PVS‑Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

close form
To get the licence for your open-source project, please fill out this form
* By clicking this button you agree to our Privacy Policy statement

close form
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

close form
check circle
Message submitted.

Your message has been sent. We will email you at


If you do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam