2022-07-28

BUG OF THE MONTH | Suspicious access to element of ‘parameters’ object by a constant index inside a loop

PVS-Studio’s warning: V3102 Suspicious access to element of ‘parameters’ object by a constant index inside a loop. OrleansGeneratedCodeHelper.cs 267

The analyzer was triggered by a loop in which an array element is accessed via the constant index. Look at the if (parameters.Length != parameterTypes.Length) condition. If it is true, the continue statement is triggered. Therefore, the collections should be of the same size to execute the subsequent code. This was most likely made to further compare pairs of corresponding elements of these collections. However, in the for body, the first element is always taken from the parameters collection.

We must say that there’s another ambiguous point. Using for is pointless since no actions are performed there except for skipping to a new iteration of this loop. Perhaps the developer expected to move to the next iteration of the external loop, but something went wrong.

This situation can be fixed by adding a flag to move to a new iteration of foreach and changing the index for parameters to i. The code will look like this:

Show more