Pour obtenir une clé
d'essai remplissez le formulaire ci-dessous
Demandez des tariffs
Nouvelle licence
Renouvellement de licence
--Sélectionnez la devise--
USD
EUR
RUB
* En cliquant sur ce bouton, vous acceptez notre politique de confidentialité

Free PVS-Studio license for Microsoft MVP specialists
To get the licence for your open-source project, please fill out this form
** En cliquant sur ce bouton, vous acceptez notre politique de confidentialité.

I am interested to try it on the platforms:
** En cliquant sur ce bouton, vous acceptez notre politique de confidentialité.

Votre message a été envoyé.

Nous vous répondrons à


Si vous n'avez toujours pas reçu de réponse, vérifiez votre dossier
Spam/Junk et cliquez sur le bouton "Not Spam".
De cette façon, vous ne manquerez la réponse de notre équipe.

>
>
>
Detecting Overflows of 32-Bit Variables…

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

22 Mar 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 Stack Overflow 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 Stack Overflow 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 Stack Overflow: "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
C++11 and 64-bit Issues

Date: 29 Avr 2014

Author: Andrey Karpov

64-bit computers have been around for a long time already. Most applications have 64-bit versions that can benefit from a larger memory capacity and improved performance, thanks to the architectural …
A Collection of Examples of 64-bit Errors in Real Programs

Date: 29 Jui 2010

Author: Andrey Karpov

This article is the most complete collection of examples of 64-bit errors in the C and C++ languages. The article is intended for Windows-application developers who use Visual C++, however, it will b…
Change of type alignment and the consequences

Date: 29 Avr 2009

Author: Andrey Karpov

When porting software one of the task a developer faces is to change types' sizes and rules of their alignments. Not so long ago we provided support of the diagnosing rule allowing you to detect data…
A talk with Jason Turner: the history of CppCast, and why it was shut down

Date: 27 Sep 2022

Author: Uliana Grishina

In this article, we are going to talk to Jason Turner, one of the CppCast founders. CppCast is the first podcast for C++ developers by C++ developers. From 2015 to 2022 CppCast had weekly conversatio…
Reworking C and C++ front-end — or how we deal with 16-year legacy code in PVS-Studio

Date: 22 Sep 2022

Author: Sergey Larin

In 2022, the PVS-Studio static analyzer for C and C++ turns 16 years old. If the analyzer were a human, it would have already finished high school! This project was started a long time ago, and since…

Comments (0)

Next comments
Unicorn with delicious cookie
Nous utilisons des cookies pour améliorer votre expérience de navigation. En savoir plus
Accepter