To get a trial key
fill out the form below
Team License (a basic version)
Enterprise License (an extended version)
* By clicking this button you agree to our Privacy Policy statement

Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

Free PVS-Studio license for Microsoft MVP specialists
* By clicking this button you agree to our Privacy Policy statement

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

I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

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.

>
>
PVS-Studio and testing Loki

PVS-Studio and testing Loki

Aug 06 2009
Author:

In PVS-Studio 3.10, support parsing of complex constructions based on templates will be improved what will allow you to efficiently search errors even in the code of those programs which use complex template libraries such as Loki. But let's start with the beginning.

Not so long ago, when our product for testing 64-bit code was called Viva64, we were addressed by the manager of the open library Loki* with an offer to check compatibility of the library with 64-bit Windows systems. For that moment the Windows-version of this library was represented only in 32-bit mode. We agreed and set about the new task bating our breath. The point is that compiler developers often use Loki library as a perfect testing model allowing you to estimate compatibility with C++ standard. We were not sure that we would be lucky. So, we consider it a great achievement that our analyzer did not hang on it, did not crash with a critical error and was able to provide normal diagnosing.

But the results of the diagnosing showed a great disadvantage of Viva64 analyzer: it cannot instantiate templates and cannot detect many errors in them. Here is an example to make it clear:

01 template <class T, class M>
02 class TemplateClass
03 {
04 public:
05   char m_char;
06   T *m_t;
07   M m_m;
08   T Get(int index) { return m_t[index]; }
09   void Set(int value) { m_t[m_m] = value; }
10 };
11
12 TemplateClass <char, int> A;

Only one potential error in line 8 was detected. In this line a variable of int type is used as an array index and it is potentially dangerous when working with large data arrays. When diagnosing this error it does not matter what relates to T type. But two other errors we will describe further have never been detected by Viva64 analyzer. One can say that Viva64 checks the code of templates rather superficially and detects only those errors which are present in it independently from the arguments with which an array will be instantiated.

This disadvantage arises from OpenCxx library on whose basis VivaCore library has been created being the base of Viva64 analyzer. The reason is very simple - OpenCxx simply cannot instantiate templates. It looks rather strange for it restricts abilities of the library. But we have a suspicion that there had been support of template instantiating in OpenCxx library but then was deliberately removed before the project became open and accessible. Some strange stubs and near-empty classes relating to template processing are an indirect evidence of this.

We have known about this drawback and its effects on diagnostics for a long time but it was Loki library that urged us to engage into implementing a mechanism for testing instantiated templates. We have created a mechanism which instantiates a template and analyzes it again on the basis of new knowledge about types. Implementation is not very good yet and needs to be improved but it already allows us to detect many new errors. Let's discuss it on the basis of the example given above.

After implementing the instantiating mechanism the analyzer immediately gave three diagnostic messages:

Line 3: error V401: Instantiate TemplateClass <char, int>: The structure's size can be decreased via changing the fields' order. The size can be reduced from 24 to 16 bytes.

Line 8: error V108: Incorrect index type for "m_t". Use memsize type instead.

Line 9: error V108: Instantiate TemplateClass <char, int> : Incorrect index type for "m_t". Use memsize type instead.

Having met the line "TemplateClass <char, int> A;" the analyzer instantiated this template and analyzed it possessing the information about T and M types. As a result, it warned that the data structure in the class is non-optimal (V401) and the size of this class in a 64-bit system can be reduced from 24 bytes to 16. For this we need to rearrange the members of the class. Pay attention that this diagnostics is possible only when you know what types the members have. For there can be no warning if, for example, m_m members have size_t type. In this case field rearranging will not help reduce the class' size.

The error in line 8 is detected as before because knowledge about T and M types does not matter here.

The last error in line 9 also relates to indexing of the array with the use of a variable of int type. Here information about M type does play an important role and is used by the analyzer.

The improvements made enabled us to perform fuller testing of Loki library. You may learn more about this work in the article "64-bit Loki" at which we are working together with the library's manager Rich Sposato.

Analysis of Loki has been performed by a special research version of Viva64. But now we decided to integrate the new abilities into the new version PVS-Studio 3.10 ** which will be released in the near future. Users will be able to use a new option DoTemplateInstantiate which enables the mode of deep template analysis.

*Note. Loki library for C++ programming language was written by Andrei Alexandrescu as part of the book "Modern C++ Design: Generic Programming and Design Patterns Applied". The library is built on template meta-programming and actively uses C++ abilities for generalized programming. (Wikipedia. Loki.)

**Note. PVS-Studio is a program product uniting and extending abilities of Viva64 and VivaMP static analyzers. Numbering of PVS-Studio begins with the version 3.00 to emphasize that this is evolution of the existing tools. In 3.00 version we did not hurry to implement template instantiating support but decided to introduce this ability in PVS-Studio 3.10.

Popular related articles
Appreciate Static Code Analysis!

Date: Oct 16 2017

Author: Andrey Karpov

I am really astonished by the capabilities of static code analysis even though I am one of the developers of PVS-Studio analyzer myself. The tool surprised me the other day as it turned out to be sma…
Technologies used in the PVS-Studio code analyzer for finding bugs and potential vulnerabilities

Date: Nov 21 2018

Author: Andrey Karpov

A brief description of technologies used in the PVS-Studio tool, which let us effectively detect a large number of error patterns and potential vulnerabilities. The article describes the implementati…
The way static analyzers fight against false positives, and why they do it

Date: Mar 20 2017

Author: Andrey Karpov

In my previous article I wrote that I don't like the approach of evaluating the efficiency of static analyzers with the help of synthetic tests. In that article, I give the example of a code fragment…
Characteristics of PVS-Studio Analyzer by the Example of EFL Core Libraries, 10-15% of False Positives

Date: Jul 31 2017

Author: Andrey Karpov

After I wrote quite a big article about the analysis of the Tizen OS code, I received a large number of questions concerning the percentage of false positives and the density of errors (how many erro…
The Ultimate Question of Programming, Refactoring, and Everything

Date: Apr 14 2016

Author: Andrey Karpov

Yes, you've guessed correctly - the answer is "42". In this article you will find 42 recommendations about coding in C++ that can help a programmer avoid a lot of errors, save time and effort. The au…
The Evil within the Comparison Functions

Date: May 19 2017

Author: Andrey Karpov

Perhaps, readers remember my article titled "Last line effect". It describes a pattern I've once noticed: in most cases programmers make an error in the last line of similar text blocks. Now I want t…
The Last Line Effect

Date: May 31 2014

Author: Andrey Karpov

I have studied many errors caused by the use of the Copy-Paste method, and can assure you that programmers most often tend to make mistakes in the last fragment of a homogeneous code block. I have ne…
Static analysis as part of the development process in Unreal Engine

Date: Jun 27 2017

Author: Andrey Karpov

Unreal Engine continues to develop as new code is added and previously written code is changed. What is the inevitable consequence of ongoing development in a project? The emergence of new bugs in th…
PVS-Studio ROI

Date: Jan 30 2019

Author: Andrey Karpov

Occasionally, we're asked a question, what monetary value the company will receive from using PVS-Studio. We decided to draw up a response in the form of an article and provide tables, which will sho…
PVS-Studio for Java

Date: Jan 17 2019

Author: Andrey Karpov

In the seventh version of the PVS-Studio static analyzer, we added support of the Java language. It's time for a brief story of how we've started making support of the Java language, how far we've co…

Comments (0)

Next comments
This website uses cookies and other technology to provide you a more personalized experience. By continuing the view of our web-pages you accept the terms of using these files. If you don't want your personal data to be processed, please, leave this site.
Learn More →
Accept