pete said:
I'm reading Malcolm as being wrong.
The restriction that pointers must be calculated within the
object, has nothing to do with comparing pointers for equality.
Example restored.
/* OK */
int x[256];
int *ptr1 = x;
int *ptr2 = x + 128;
while(ptr2 != ptr1)
ptr2--;
/* silly game, may work as expected may go horribly wrong */
int x[128];
int y[128];
int *ptr1 = x;
int *ptr2 = y;
while(ptr2 != ptr1)
ptr2--;
These could easily compile to exactly the same machine code.
What the hacker who wrote the second example is relying on is that
array y is probably next to array x in memory. As he decrements,
ptr2 will eventually count down to ptr1, which is the start of x.
If we write a value as we loop, array x will be set.
Unfortunately, it could happen that x and y are in different
segments on a DOS machine. Therefore the array will be set, as
before, but the loop won't terminate properly because ptr1 and ptr2
don't compare as equal, despite pointing to the same physical
location. This only happens because we've managed to defeat C's
pointer management system. It can't happen if we do things properly,
as in example 1.