to the top
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 haven't received our response, please do the following:
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.

>
>
>
C# Programmer, it's time to test yourse…

C# Programmer, it's time to test yourself and find error

Feb 01 2021

The PVS-Studio analyzer is regularly updated with new diagnostic rules. Curiously enough, diagnostics often detect suspicious code fragments before the end of the work. For example, such a situation may happen while testing on open-source projects. So, let's take a look at one of these interesting findings.

0792_StringBuilder_BounceCastle/image1.png

As mentioned earlier, one of the stages of diagnostic rule testing is to check its operation on a real codebase. To that end, we have a set of selected open-source projects that we use for the analysis. The obvious advantage of this approach is the ability to see the diagnostic rule behavior in real conditions. There's also a less obvious advantage. Sometimes you may find such an interesting case, so it would be a sin not to write an article about it. :)

Now, let's take a look at the code from the Bouncy Castle C# project and find the error in it:

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();
}

For those who like to cheat and peek, I added a picture to keep you guessing.

0792_StringBuilder_BounceCastle/image2.png

I'm sure some of you couldn't see the error without using the IDE or the StringBuilder class documentation. The error happened when calling the constructor:

StringBuilder sb = new StringBuilder('[');

Actually, this is exactly what the PVS-Studio static analyzer warns us about: 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.

The programmer wanted to create an instance of the StringBuilder type, where the string begins with the '[' character. However, due to a typo, we'll have an object without any characters with a capacity of 91 elements.

This happened because the programmer used single quotes instead of double-quotes. That's why the wrong constructor overload was called:

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

When the constructor is called, the '[' character literal will be implicitly cast to the corresponding value of the int type (91 in Unicode). Because of this, the constructor with the int type parameter setting the initial capacity will be called. Although, the programmer wanted to call the constructor which sets the string's beginning.

To fix the error, the developer has to replace the character literal with a string literal (i.e., use "[" instead of '['). It will cause the correct constructor overload.

We decided to go the extra mile and expanded the cases reviewed by diagnostic. As a result, in addition to character literals, some other expressions of the char type are considered now. We also check methods in the same way.

The diagnostic described above was added in the PVS-Studio 7.11 release. You can download the analyzer latest version yourself. You'll see what the V3165 diagnostic can do, as well as other diagnostics for C, C++, C#, and Java.

By the way, the users themselves often suggest some ideas of diagnostics to us. This time it has happened thanks to the user of Krypt from Habr. If you also have some ideas for diagnostic rules - don't hesitate to reach out to us!

P.S. This error has already been fixed in the current project code base. Though, this doesn't change the fact that it has existed in the code for some time, and that static analysis allows you to identify such problems and fix them at the earliest stages.

Popular related articles
Converting string to enum at the cost of 50 GB: let's analyze the CVE-2020-36620 vulnerability

Date: Mar 21 2023

Author: Sergey Vasiliev

In this article, we're going to discuss the CVE-2020-36620 vulnerability and see how a NuGet package for converting string to enum can make a C# application vulnerable to DoS attacks.
Should we check libraries before using them? MudBlazor helps us find the answer

Date: Feb 10 2023

Author: Nikita Panevin

There was a need in our company to use a library for Blazor components. We chose MudBlazor and checked its code quality before implementation. The result is a number of strange things and even a repr…
Under the hood of SAST: how code analysis tools look for security flaws

Date: Jan 26 2023

Author: Sergey Vasiliev

Here we'll discuss how SAST solutions find security flaws. I'll tell you about different and complementary approaches to detecting potential vulnerabilities, explain why each of them is necessary, an…
Wave Function Collapse for procedural generation in Unity

Date: Jan 24 2023

Author: Andrey Moskalev

Wave Function Collapse is an algorithm that can generate anything by arranging it according to rules or samples. In this article, we are going to look at how to use WFC to generate a map in Unity.
PVS-Studio now analyzes Blazor components

Date: Jan 10 2023

Author: Aleksey Avdeev

Hello everyone. This is a small article about how PVS-Studio analyzes Blazor components. We'll try to anticipate your questions and answer them here. Enjoy!

Comments (0)

Next comments next comments
close comment form
Unicorn with delicious cookie
Our website uses cookies to enhance your browsing experience.
Accept