Well, not really, but here's a code problem that confounded some really smart devs – and it looks so simple!
void IncPtr( unsigned int cElements )
if( m_pMax - m_pCurrent > cElements )
m_pCurrent += cElements;
OK, so here's the question – if an error has happened, and m_pCurrent is > m_pMax, which implies the difference in the pointers is negative, which code branch do we execute? Assume cElements is a reasonably small number.
Hmmm – the immediate answer would be that the difference gets cast to an unsigned int to be compared with cElements, if it is negative, then it becomes large, and it is not less than cElements, so we throw, so this code is safe, right?
The answer, unfortunately, is a solid maybe. Back in engineering school, I got acquainted with something called dimensional analysis where you worked something out based on dimensions. For example, if you want to know how to get gallons from some number of miles and miles/gallon, figuring (miles / (miles/gallon)) shows the answer is in gallons. A similar approach for integers is often helpful. Let's look at the types involved in the if statement. First, what is the type of a pointer difference? That's a ptrdiff_t – which is a signed number that is the same number of bytes as a pointer, which means that on a 64-bit build, it is an __int64, and on a 32-bit build, it is a 32-bit int.
What we now have is:
If( ptrdiff_t < unsigned int)
If you have a 32-bit build, this works out to:
If( int < unsigned int)
Which then implies:
If((unsigned int)int < unsigned int)
The cast gives you an implied check for the lhs being less than zero (assuming reasonably small values for the unsigned number), and negative numbers will now fail, and since this is an error, this is what we want, and life is good. Do note that if you're compiling with all the warnings on, this will cause a warning, which you'd probably ignore, or cast away, being oblivious to the impending doom that is approaching.
Now consider 64-bit:
If(__int64 < unsigned int)
This gets cast very differently…
If( __int64 < (__int64)unsigned int)
You won't get a warning on your 64-bit build because the cast from unsigned int (like the cast from unsigned short to int) preserves value, and the assumption is that the comparison will always be correct. Under the error condition outlined here, the problem is that we're now not catching the error, we'll add to a pointer that's already probably bogus, and things will get worse from here.
As you can see, which branch gets executed depends on whether you're building 32 or 64-bit!
The solutions and lessons are –
- Be wary of pointer math in porting code to 64-bit – ptrdiff_t is negative, and changes size.
- Using the bit flipping of negative to very large positive effect is really programming with side effects, and being clever, neither of which are good practices.
- Explicitly determine which path to take when dealing with negative numbers in a comparison
BTW, SafeInt will be available very shortly on CodePlex – I have just one more internal hoop to jump before I can post it.