Our website uses cookies to enhance your browsing experience.
Accept
to the top
>
>
>
Interprocedural context-sensitive analy…

Interprocedural context-sensitive analysis

Aug 15 2024

Static code analyzers can take into consideration the data exchanged between procedures/functions, helping to detect more errors and potential vulnerabilities. This is crucial for identifying issues like memory leaks, null pointer dereferences, and array overruns, as these errors often arise when resource creation, usage, and release are handled by different functions.

To detect errors that result from the interaction of multiple functions, static analyzers use interprocedural context-sensitive code analysis.

We'll explore it in detail by examining a real error that PVS-Studio detected in the AvalonStudio source code (C#).

Let's look at the IsBuiltInType function first. Note that if its parameter, cursor, turns to be a null reference, the function returns false.

private static bool IsBuiltInType(ClangType cursor)
{
  var result = false;
  if (cursor != null && ....)
  {
    return true;
  }
  return result;
}

Take a look at another code fragment where the above function is called:

private static StyledText InfoTextFromCursor(ClangCursor cursor)
{
  ....
  if (cursor.ResultType != null)
  {
    result.Append(cursor.ResultType.Spelling + " ",
                  IsBuiltInType(cursor.ResultType) ? theme.Keyword 
                                                   : theme.Type);
  }
  else if (cursor.CursorType != null)
  {
    switch (kind)
    {
      ....
    }
    result.Append(cursor.CursorType.Spelling + " ",
                  IsBuiltInType(cursor.ResultType) ? theme.Keyword
                                                   : theme.Type);
  }
  ....
}

If cursor.ResultType != null, the body of the first if statement is executed. So, if control is passed inside the body of the second if, statement, we know for sure that cursor.ResultType reference is null.

Let's examine the spot where the IsBuiltInType function (which we discussed earlier) is called:

result.Append(cursor.CursorType.Spelling + " ",
              IsBuiltInType(cursor.ResultType) ? theme.Keyword
                                               : theme.Type);

The analyzer recognizes that cursor.ResultType is a null reference. So, it concludes that with this argument, the function always returns false.

That's what the interprocedural context-sensitive analysis is all about.

The ternary operator condition that is always false seems suspicious, so the analyzer issues a warning:

V3022 Expression 'IsBuiltInType(cursor.ResultType)' is always false.

The analyzer is right. If we examine the code more closely, we'll notice a typo. In the body of the second if statement, the cursor.CursorType variable should be passed as the argument when calling the IsBuiltInType function.

The fixed code:

private static StyledText InfoTextFromCursor(ClangCursor cursor)
{
  ....
  if (cursor.ResultType != null)
  {
    result.Append(cursor.ResultType.Spelling + " ",
                  IsBuiltInType(cursor.ResultType) ? theme.Keyword 
                                                   : theme.Type);
  }
  else if (cursor.CursorType != null)
  {
    switch (kind)
    {
      ....
    }
    result.Append(cursor.CursorType.Spelling + " ",
                  IsBuiltInType(cursor.CursorType) ? theme.Keyword
                                                   : theme.Type);
  }
  ....
}

Analyzing the interaction of functions located in different translation units or program modules adds complexity. In this case, it is referred to as intermodular analysis.

Additional links

Popular related articles


Comments (0)

Next comments next comments
close comment form
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 do not see the email in your inbox, please check if it is filtered to one of the following folders:

  • Promotion
  • Updates
  • Spam