How PVS-Studio Found an Error in the Library Used in PVS-Studio

DZone 's Guide to

How PVS-Studio Found an Error in the Library Used in PVS-Studio

This is a short story about how PVS-Studio helped us find an error in the source code of the library used in PVS-Studio.

· Web Dev Zone ·
Free Resource

This is a short story about how PVS-Studio helped us find an error in the source code of the library used in PVS-Studio. And it was not a theoretical error but an actual one — the error appeared in practice when using the library in the analyzer.

PVS studio

In PVS-Studio_Cmd (as well as some other utilities), we use a special library for parsing command line arguments: CommandLine.

Today, I supported the new mode in PVS-Studio_Cmd, and it so happened that I had to use this library for parsing command line arguments. While writing the code, I had to debug it because I was working with unfamiliar APIs.

So, the code was written, compiled, executed and...

Unhandled exception

Code execution goes inside the library where an exception of the NullReferenceException type occurs. It was not so clear from my point of view, as I didn't pass any null references into the method.

To make sure, I looked at the comments on the callee method. It is hardly probable that they described the conditions of occurrence of an exception of the NullReferenceException type (as it seems to me usually exceptions of this type are not provided for).

Comments of callee method

There was no information about NullReferenceException in the comments to the method (which, unfortunately, was expected).

To see what exactly caused the exception (and where it occurs), I decided to download the project's source code, build it, and add a reference to the debug version of the library to the analyzer. The source code of the project is available at GitHub. We needed version 1.9.71 of the library. It is the one used in the analyzer now.

I downloaded the corresponding version of the source code, built the library, added a reference to the debug library to the analyzer, and executed the code. I saw:


So, the place where the exception occurs was clear — helpInfo has a null value, which caused an exception of the NullReferenceException type when accessing the Left property.

I started thinking about it. Recently, PVS-Studio for C# has been well improved in various aspects, including the search for dereferencing of potentially null references. In particular, the interprocedural analysis was improved in a number of ways. That's why I was immediately interested in checking the source code to understand if PVS-Studio could find the aforementioned error.

I checked the source code, and among other warnings, I saw exactly what I hoped for:

"PVS-Studio warning: V3080 Possible null dereference inside method at 'helpInfo.Left'. Consider inspecting the 2nd argument: helpInfo. Parser.cs 405"

This was it! That's exactly what we needed. Let's take a more detailed look at the source code.

private bool DoParseArgumentsVerbs(
  string[] args, object options, ref object verbInstance)
  var verbs 
    = ReflectionHelper.RetrievePropertyList<VerbOptionAttribute>(options);
  var helpInfo 
    = ReflectionHelper.RetrieveMethod<HelpVerbOptionAttribute>(options);
  if (args.Length == 0)
    if (helpInfo != null || _settings.HelpWriter != null)
      DisplayHelpVerbText(options, helpInfo, null); // <=

    return false;

The analyzer issues a warning for calling the DisplayHelpVerbText method and warns about the second argument — helpInfo. Pay attention that this method is located in the then-branch of the if statement. The conditional expression is composed in such a way that the then-branch can be executed at the next values of the variables:

  • helpInfo == null.
  • _settings.HelpWriter != null.

Let's see the body of the DisplayHelpVerbText method:

private void DisplayHelpVerbText(
  object options, Pair<MethodInfo, 
  HelpVerbOptionAttribute> helpInfo, string verb)
  string helpText;
  if (verb == null)
    HelpVerbOptionAttribute.InvokeMethod(options, helpInfo, null, out helpText);
    HelpVerbOptionAttribute.InvokeMethod(options, helpInfo, verb, out helpText);

  if (_settings.HelpWriter != null)

Since verb == null (see the method call) we are interested in then-branch of the if statement. Although the situation is similar to the else branch, let's consider the then-branch because in our particular case the execution went through it. Remember that helpInfo may be null.

Now, let's look at the body of the HelpVerbOptionAttribute.InvokeMethod method. Actually, you have already seen it in the above screenshot:

internal static void InvokeMethod(
    object target,
    Pair<MethodInfo, HelpVerbOptionAttribute> helpInfo,
    string verb,
    out string text)
  text = null;
  var method = helpInfo.Left;
  if (!CheckMethodSignature(method))
    throw new MemberAccessException(

  text = (string)method.Invoke(target, new object[] { verb });

helpInfo.Left is called unconditionally, while helpInfo may be null. The analyzer warned about it, and that's what happened.


It is nice that we managed to find an error in the source code of the library used in PVS-Studio with the help of PVS-Studio. I think this is a kind of the answer to the question, "Does PVS-Studio find errors in PVS-Studio source code?" The analyzer can find errors not only in the PVS-Studio code but also in the code of the libraries.

Finally, I suggest you download the analyzer and try to check your project — what if you can find something interesting there too?

bugs, code quality, csharp, dotnet, programming, pvs-studio, software development, static analysis

Published at DZone with permission of Sergey Vasiliev . See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}