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

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
Request our prices
New License
License Renewal
--Select currency--
USD
EUR
GBP
RUB
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
I am interested to try it on the platforms:
* By clicking this button you agree to our Privacy Policy statement

** This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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.

>
>
>
Detecting Overflows of 32-Bit Variables…

Detecting Overflows of 32-Bit Variables in Long Loops in 64-Bit Programs

Mar 22 2016
Author:

One of the problems that 64-bit software developers have to face is overflows of 32-bit variables in very long loops. PVS-Studio code analyzer is very good at catching issues of this type (see the Viva64 diagnostic set). A lot of questions concerning variable overflows are asked at StackOverflow.com. But since my answers may be treated as pure advertisement, rather than useful reference information, I decided to write an article where I could talk about PVS-Studio's capabilities.

A loop is a typical C/C++ construct. When porting software to the 64-bit architecture, loops suddenly become problem spots, as few developers think in advance what would happen if the program had to execute billions of iterations.

In our articles we call such issues 64-bit errors. Actually, these are simple errors. What makes them special is that they manifest themselves only in 64-bit applications. You simply don't have such long loops in 32-bit programs, and it's impossible to create an array of size larger than INT_MAX.

So, we've got a problem: 32-bit types overflow in a 64-bit program. 32-bit types include int, unsigned, and long (if you're working on Win64). We need to find a way to detect all such dangerous spots. The PVS-Studio analyzer can do it, and it is what we are going to talk about.

Let's discuss different scenarios of variable overflows occurring in long loops.

Scenario one. See the corresponding topic at StackOverflow here: "How can elusive 64-bit portability issues be detected?". We have the following code:

int n;
size_t pos, npos;
/* ... initialization ... */
while((pos = find(ch, start)) != npos)
{
    /* ... advance start position ... */
    n++; // this will overflow if the loop iterates too many times
}

This program processes very long strings. In a 32-bit program, a string can't be of length larger than INT_MAX, so no errors of this kind can occur there. Yes, the program can't process large amounts of data, but it's just limitation of the 32-bit architecture, not a bug.

In a 64-bit program, however, the length of a string can exceed INT_MAX; therefore, the n variable may overflow. The result is undefined behavior. It's a wrong belief that an overflow would simply turn the number 2147483647 into -2147483648. It is literally undefined behavior: you can't predict the consequences. If you don't believe that an overflowed signed variable can cause unexpected changes in program execution, please see my article "Undefined behavior is closer than you think".

OK, we need to check if the n variable can overflow. No problem – we run PVS-Studio on this code and get the following message:

V127 An overflow of the 32-bit 'n' variable is possible inside a long cycle which utilizes a memsize-type loop counter. mfcapplication2dlg.cpp 190

Changing the type of the n variable to size_t will make the error – and the message – disappear.

In the same topic, one more code example is discussed that needs to be checked:

int i = 0;
for (iter = c.begin(); iter != c.end(); iter++, i++)
{
    /* ... */
}

Again, we run PVS-Studio and get warning V127:

V127 An overflow of the 32-bit 'i' variable is possible inside a long cycle which utilizes a memsize-type loop counter. mfcapplication2dlg.cpp 201

That topic at StackOverflow also brings up the question what one should do when the code base is huge and one needs to find all errors of this kind.

As we have already seen, the PVS-Studio static code analyzer can catch these bugs. Moreover, it's the only way to cope with a large project. The analyzer also provides a convenient user interface to work with multiple diagnostic messages. You can use interactive filters on messages, mark them as false positives, and so on. However, description of PVS-Studio's capabilities is beyond the scope of this article. If you want to learn more about the tool, please see the following resources:

By the way, we also have had an experience of porting a large project of 9 million LOC to the 64-bit platform. And PVS-Studio has done that task rather well.

Let's see another topic at StackOverflow: "Can Klocwork (or other tools) be aware of types, typedefs and #define directives?".

As far as I understand, the programmer has set out to find a tool that could spot all the loops with 32-bit counters. In other words, all the loops where type int is used.

This task is somewhat different from the previous one. But such loops do need to be found and fixed, as you can't use a variable of type int to process huge arrays, and all.

However, the person chose a wrong approach. But it wasn't his fault; he simply didn't know about PVS-Studio. You'll see what I mean in a moment.

So, what he wants to search for is the following construct:

for (int i = 0; i < 10; i++)
    // ...

It's horrible. You would have to look through an enormous number of loops to figure out if they can cause a bug or not. It's a huge amount of work, and I doubt anyone could do it staying focused all the way. So, missing a lot of dangerous fragments seems inevitable.

On the other hand, fixing every single loop by replacing int with, say, intptr_t is not a good idea either. This approach involves too much work and too many changes in the code.

The PVS-Studio analyzer can help here. It won't find the loop from the example above – because it doesn't have to. That loop simply has no room for a bug, as it executes only 10 iterations and will never end up with an overflow. We don't need to waste our time checking that code.

But what the analyzer can find are loops like this one:

void Foo(std::vector<float> &v)
{
  for (int i = 0; i < v.size(); i++)
    v[i] = 1.0;
}

The tool will generate two warnings at once. The first tells us that a 32-bit type is being compared with a memsize-type:

V104 Implicit conversion of 'i' to memsize type in an arithmetic expression: i < v.size() mfcapplication2dlg.cpp 210

Indeed, the i variable's type is not suited for long loops.

The second warning tells us that it is strange to use a 32-bit variable for indexing. If the array is large, the code is incorrect.

V108 Incorrect index type: v[not a memsize-type]. Use memsize type instead. mfcapplication2dlg.cpp 211

The fixed code should look like this:

void Foo(std::vector<float> &v)
{
  for (std::vector<float>::size_type i = 0; i < v.size(); i++)
    v[i] = 1.0;
}

It has become long and ugly, so you may feel tempted to use the auto keyword. But you can't because doing so would make the code incorrect again:

for (auto i = 0; i < v.size(); i++)
  v[i] = 1.0;

Since the constant 0 is of type int, the i variable would be of type int as well. That is, we would end up where we started. By the way, since we've started talking about new features of the C++ standard, I recommend reading the article "C++11 and 64-bit Issues".

I think we could make a trade-off and write a version of that code that is not perfect but still correct:

for (size_t i = 0; i < v.size(); i++)
  v[i] = 1.0;

Note. Of course, an even better solution would be to use iterators or the fill() algorithm, but we are talking about searching for overflows of 32-bit variables in old programs. That's why I don't discuss those fixing techniques in this article - they're just from a different story.

Note that the analyzer is pretty smart and tries not to bother you without a good reason. For example, it won't generate the warning on seeing a code fragment where a small array is processed:

void Foo(int n)
{
  float A[100];
  for (int i = 0; i < n; i++)
    A[i] = 1.0;
}

Conclusion

PVS-Studio analyzer is the leader as far as 64-bit bug search goes. After all, it was originally conceived and created exactly as a tool to help programmers in porting their software to 64-bit systems, and was known as Viva64 at that time. It was only some time later that it turned into a general-purpose analyzer, but the 64-bit diagnostics have always been there, ready to help you out.

Download the demo version here.

Find out more about 64-bit software development.

Popular related articles
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…
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…
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…
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…
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…
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…
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…
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…
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…
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…

Comments (0)

Next comments

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.
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