Fun with printf
fprintf(usageOutputFile, “… (200 pixels across, 50% of height).\n”);
Output:
… (200 pixels across, 5027777772734f height).
Guess the bug!
fprintf(usageOutputFile, “… (200 pixels across, 50% of height).\n”);
Output:
… (200 pixels across, 5027777772734f height).
Guess the bug!
November 17th, 2006 at 12:04:12
“% of” will be interpreted as a format string for a floating point value, thus printing an arbitrary point in memory as a float.
November 17th, 2006 at 12:09:11
Close, but not quite.
November 17th, 2006 at 12:13:55
Oh, right, I’m a retard. It’s printing a random memory address as an octal value. The f just randomly threw me.
November 17th, 2006 at 12:14:52
Correct!
November 17th, 2006 at 12:27:21
Just use ‘%%’ to escape the ‘%’ in a format string ;-)
November 17th, 2006 at 12:39:24
Yes, that’s the solution to the bug. But the point of this post was the diagnosis, not the fix. :)
Thanks, though.
December 25th, 2006 at 01:00:34
“random memory address”
Nothing random about it. It’s printing, in octal, the integer value of the stack location above the second argument. According to the OS X ppc32 ABI (and I believe the i686 and ppc64, but I’m too lazy to check), this would correspond to the local variables area of the calling function (i.e. the function where this fprintf statement occurs). If there are no local variables, it will be in the saved registers area.
So basically, it’s printing the octal representation of the integer cast of sizeof(int) of a local variable, or if none, then one of the saved registers.
I don’t know what this stack location corresponds to in other ABIs, but it’ll probably be something similar.
But the point here is it’s definitely not random.