Fun with printf

2006-11-17 11:04:49 -08:00

fprintf(usageOutputFile, “… (200 pixels across, 50% of height).\n”);

Output:

… (200 pixels across, 5027777772734f height).

Guess the bug!

7 Responses to “Fun with printf”

  1. Augie Fackler Says:

    “% of” will be interpreted as a format string for a floating point value, thus printing an arbitrary point in memory as a float.

  2. Peter Hosey Says:

    Close, but not quite.

  3. Augie Fackler Says:

    Oh, right, I’m a retard. It’s printing a random memory address as an octal value. The f just randomly threw me.

  4. Peter Hosey Says:

    Correct!

  5. Hub Says:

    Just use ‘%%’ to escape the ‘%’ in a format string ;-)

  6. Peter Hosey Says:

    Yes, that’s the solution to the bug. But the point of this post was the diagnosis, not the fix. :)

    Thanks, though.

  7. Kevin Ballard Says:

    “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.

Leave a Reply

Do not delete the second sentence.


Warning: Undefined array key "ntt_saved_comment_text" in /home/public/blog/wp-content/plugins/negative-turing-test/negative-turing-test.php on line 143