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:
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.
I often hear in various interpretations the phrase: "The given examples show not the code incorrect from the viewpoint of porting to x64 systems, but the code incorrect in itself". I would like to discuss and theorize a bit on this point in the blog. Please, take this note with a bit of humor.
In C language, you may use functions without defining them. Pay attention that I speak about C language, not C++. Of course, this ability is very dangerous. Let us have a look at an interesting example of a 64-bit error related to it. Below is the correct code that allocates and uses three arrays, 1 GB each:.
In this article, I've collected a huge amount of links all of which could prove potentially useful to software developers who are getting acquainted with 64-bit world. In general, we will cover C/C++ languages, but I am sure, that developers from other languages will discover a lot of interesting stuff here as well.