64-bit Errors Are Here at Last
As a person not indifferent to 64-bit errors, I decided to write a small comment to the article “Buggy Security Guidance from Apple”.
Many applications have been already ported to 64 bits and seem to be working well. However, 64-bit errors still inhabit many of them, lying in wait. Only a very attentive programmer can notice them. Or the PVS-Studio code analyzer.
I have recently stumbled across an interesting article titled “Buggy Security Guidance from Apple”.
Before going on to read my comment, please read the article first. What I found of interest there is the fact that after having accomplished huge work and successfully implemented checks for overflows one might easily make a mistake in some very simple thing — in particular, multiply two ‘int’ variables and write the result into a ‘size_t’ variable.
I mean the following line:
size_t bytes = n * m;
It is recommended to be replaced with this one:
size_t bytes = (size_t)n * (size_t)m;
This is a very common type of 64-bit errors, awfully difficult to notice and detect.
The world seems to start facing at last what I already described several years ago in my articles and manual on 64-bit software development. So I do recommend you to read those materials:
- A Collection of Examples of 64-bit Errors in Real Programs
- A 64-bit horse that can count
- Development of 64-bit C/C++ applications
But what is most important, it’s not enough to be simply aware of these errors. True, you will make sure your new code is written correctly. But what about the old code? How many defects of that kind does it have and how to catch them?
I’ll give you a hint. You need to use the PVS-Studio analyzer — or rather, its set of 64-bit diagnostics.
To get the proof that the error with an overflow is a common one, take a look at the warning the analyzer generates on the line “size_t bytes = n * m;”.
The diagnostic message: V101 Implicit assignment type conversion to memsize type.
Notice its number — V101. It was the very first 64-bit diagnostic rule that we had implemented. The dangers programmers are talking about nowadays were obvious to us long ago. So don’t hesitate: PVS-Studio is a perfect tool for catching 64-bit bugs.
Note. Be prepared for numbers of false positives generated by 64-bit diagnostics. They can’t be avoided. The analyzer does not know if the program counts the number of days in a month or computes the size of some big file. It happens quite frequently that PVS-Studio can’t understand what values are stored in variables and if an overflow will occur. Unfortunately, there is no better alternative to be found around anyway. PVS-Studio is the leader in the area of 64-bit error detection. And it also offers lots of false positive suppression mechanisms.
Article republished by the author’s permission.