C # programmer, test yourself - find the error

The PVS-Studio analyzer is regularly updated with new diagnostic rules. Interestingly, diagnostics often detect suspicious code fragments before the end of all work. For example, during testing on open-source projects. One of these interesting 'finds' I would like to share with you today.





As mentioned above, one of the stages of testing a diagnostic rule is checking its operation on a real code base. For this, a set of selected open-source projects is used, for which the analysis is carried out. The obvious advantage of this approach is the ability to see how the diagnostic rule behaves in "real conditions". Less obvious - sometimes there is something so interesting that it's not a sin to write a note about it.





, Bouncy Castle C# :





public static string ToString(object[] a)
{
  StringBuilder sb = new StringBuilder('[');
  if (a.Length > 0)
  {
    sb.Append(a[0]);
    for (int index = 1; index < a.Length; ++index)
    {
      sb.Append(", ").Append(a[index]);
    }
  }
  sb.Append(']');
  return sb.ToString();
}

      
      



, , , .





, IDE StringBuilder. :





StringBuilder sb = new StringBuilder('[');

      
      



, PVS-Studio: V3165 Character literal '[' is passed as an argument of the 'Int32' type whereas similar overload with the string parameter exists. Perhaps, a string literal should be used instead. Arrays.cs 193.





StringBuilder, '['. - 91 , .





- , , :





....
public StringBuilder(int capacity);
public StringBuilder(string? value);
....

      
      



'[' int (91 Unicode). int, . , .





(.. "[", '['), .





, . , , char, , , .





, , PVS-Studio 7.11. V3165, C, C++, C# Java.





, . - Krypt Habr. - !





P.S. . , - , .





, : Valery Komarov. C# Programmer, It's Time to Test Yourself and Find Error.








All Articles