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.

>
>
>
Search of 64-bit errors in array implem…

Search of 64-bit errors in array implementation

Jan 11 2010
Author:

In PVS-Studio 3.43, we revised the way how Viva64 analyzer detects errors in the classes serving as containers (arrays). Before, we have stuck to the principle that if a class has operator[], its parameter must have memsize-type (ptrdiff_t, size_t) and not int or unsigned. We still recommend that you use memsize type as an argument for operator[]. It allows the compiler to build a more efficient code in some cases and avoid some 64-bit errors beforehand. Now we have changed the approach to working with classes that have operator[] what allows us to reduce the number of unnecessary diagnostic warnings.

Let us consider an example that might contain an error if we want to work with large data amounts:

class MyArray {
  std::vector <float> m_arr;
  ...
  float &operator[](int i)
  {
    return m_arr[i];
  }
} A;
...
int x = 2000;
int y = 2000;
int z = 2000;
A[x * y * z] = 33;

The first drawback of this code is that operator[] does not allow us to access the item with the number more than INT_MAX.

Note. I would like to clarify one important thing. In the release-version, for a code like the one in the example, the compiler can provide an optimization that will work because the 64-bit register will be used to calculate and pass the index. I will make a separate post to examine this example more thoroughly. But this luck does not make the code correct. You may learn more about dangerous optimizations here.

The second drawback of the code lies in the expression x*y*z where an overflow might occur when working with a large array.

Before, the analyzer has generated two warnings (V108). The first is using int type when calling the array m_arr. The second is using int type when calling the array A. Although operator[] of the class MyArray takes an int argument, we offered to use a memsize-type as the index. When the programmer changed the types of the variables x, y and z to ptrdiff_t, Visual C++ compiler started warning about type conversion in the line A[x * y * z] = 33:

warning C4244: 'argument' : conversion from 'ptrdiff_t' to 'int', possible loss of data

This warning prompted the user to change the argument in operator[] and the code became absolutely correct. Here is an example of the corrected code:

class MyArray {
  std::vector <float> m_arr;
  ...
  float &operator[](ptrdiff_t i)
  {
    return m_arr[i];
  }
} A;
...
ptrdiff_t x = 2000;
ptrdiff_t y = 2000;
ptrdiff_t z = 2000;
A[x * y * z] = 33;

Unfortunately, this diagnosis approach has one great drawback. In some cases, operator[] cannot be changed or using int as the index is absolutely justified. And it appeared that Viva64 analyzer generated a lot of unnecessary warnings. CString class from MFC can serve as an example. The operator in CString class has the prototype:

TCHAR operator []( int nIndex ) const;

Because of this the code is diagnosed as dangerous:

int i = x;
CString s = y;
TCHAR c = s[i];

CString class is inaccessible to edit. And, well, hardly will anyone use CString type in a standard program to work with lines longer than two milliard characters. In its turn, Viva64 analyzer generated many warnings on this code. If the programmer changed the index's type from int to ptrdiff_t, it was the compiler that generated the warnings. We could use warning suppression //-V108, but it would overload the code. You may learn more about warning suppression in the article: PVS-Studio: using the function "Mark as False Alarm".

We made a decision to consider the construct A[x * y * z] = 33; from the first example safe. Now, if operator[] takes a 32-bit type as an argument (for example, int) and we call this operator also using a 32-bit type, this call is considered safe.

Of course, it might hide an error. That is why, we added a new diagnostic warning V302: "Member operator[] of 'FOO' class has a 32-bit type argument. Use memsize-type here". This diagnostic warning is generated for operator[] defined with a 32-bit argument.

The smartness of this solution consists in that this warning is not generated on the library code that is not accessible to change. I.e., V302 warning will not be generated for the class CString but will be for the user class MyArray.

If operator[] in MyArray class is correct and really should have the type int, the programmer will only need to write only one warning suppression //-V302 in this class instead of multiple places where it is used.

The last change related to array processing concerns introduction of one more warning V120: "Member operator[] of object 'FOO' declared with 32-bit type argument, but called with memsize type argument". In whole, this warning copies the compiler warning about converting a 64-bit type to a 32-bit one. It will be useful when there are a lot of warnings generated by the compiler and among them you miss the information about code efficiency on a 64-bit system.

Popular related articles
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…
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 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…
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…
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…
Free PVS-Studio for those who develops open source projects

Date: Dec 22 2018

Author: Andrey Karpov

On the New 2019 year's eve, a PVS-Studio team decided to make a nice gift for all contributors of open-source projects hosted on GitHub, GitLab or Bitbucket. They are given free usage of PVS-Studio s…
How PVS-Studio Proved to Be More Attentive Than Three and a Half Programmers

Date: Oct 22 2018

Author: Andrey Karpov

Just like other static analyzers, PVS-Studio often produces false positives. What you are about to read is a short story where I'll tell you how PVS-Studio proved, just one more time, to be more atte…
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 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…
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…

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