V3025. Incorrect format. Consider checking the N format items of the 'Foo' function.

The analyzer has detected a possible error related to use of formatting methods: String.Format, Console.WriteLine, Console.Write, etc. The format string does not correspond with actual arguments passed to the method. Here are some simple examples:

Unused arguments.

int A = 10, B = 20;
double C = 30.0;
Console.WriteLine("{0} < {1}", A, B, C);

Format item {2} is not specified, so variable 'C' won't be used.

Possible correct versions of the code:

//Remove extra argument
Console.WriteLine("{0} < {1}", A, B);

//Fix format string
Console.WriteLine("{0} < {1} < {2}", A, B, C);

Number of arguments passed is less than expected.

int A = 10, B = 20;
double C = 30.0;
Console.WriteLine("{0} < {1} < {2}", A, B);
Console.WriteLine("{1} < {2}", A, B);

A much more dangerous situation occurs when a function receives fewer arguments than expected. This will raise a FormatException exception.

Possible correct versions of the code:

//Add missing argument
Console.WriteLine("{0} < {1} < {2}", A, B, C);

//Fix indices in format string
Console.WriteLine("{0} < {1}", A, B);

The analyzer doesn't output the warning given that:

  • The number of format items specified matches the number of arguments.
  • The format object is used a number of times:
int row = 10;
Console.WriteLine("Line: {0}; Index: {0}", row);

Here is an example of this bug in a real-life application:

var sql = string.Format(
  " OVER (ORDER BY {2}) AS Row, {0} FROM {3} {4}) AS Paged ",
  columns, pageSize, orderBy, TableName, where);

The function receives 5 formatting objects, but the 'pageSize' variable is not used as format item {1} is missing.

According to Common Weakness Enumeration, potential errors found by using this diagnostic are classified as CWE-685.

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

Do you make errors in the code?

Check your code
with PVS-Studio

Static code analysis
for C, C++, and C#

goto PVS-Studio;