## Fill out the form in 2 simple steps below:

Step 1

Step 2
** By clicking this button you agree to our Privacy Policy statement
Request our prices
--Select currency--
USD
EUR
* By clicking this button you agree to our Privacy Policy statement

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

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

I am interested to try it on the platforms:
 Windows Linux macOS PVS-Studio for .NET Core JetBrains Rider
* By clicking this button you agree to our Privacy Policy statement

Message submitted.

Your message has been sent. We will email you at

check your Spam/Junk folder and click the "Not Spam" button for our message.
This way, you won't miss messages from our team in the future.

>
>
>
V3157. Suspicious division. Absolute va…
Introduction
Analyzing projects
On Windows
On Linux and macOS
Cross-platform
IDE
Build systems
Game Engines
Continuous use of the analyzer in software development
Deploying the analyzer in cloud Continuous Integration services
Managing analysis results
Analyzer diagnostics
General Analysis (C++)
General Analysis (C#)
General Analysis (Java)
Micro-Optimizations (C++)
Micro-Optimizations (C#)
Diagnosis of 64-bit errors (Viva64, C++)
Customer specific requests (C++)
MISRA errors
AUTOSAR errors
OWASP errors (C++)
OWASP errors (C#)
OWASP errors (Java)
Problems related to code analyzer
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.