Re: libc/printf bug



"bill pursell" <bill.pursell@xxxxxxxxx> writes:

The following code exhibits unexpected behavior. Either it's
a bug in libc, or the author (me) is blind as a bat. Am I missing
something here, or is something horribly wrong? It appears
that printf is mangling the address unless the second argument
gets cast. I don't see that the cast should have any effect at all
on the output. Any thoughts?

[tmp]$ cat printf_bug.c

int
main()
{

float a=0;

printf("%p\n", &a);
printf("%x %p\n", a, &a);
printf("%x %p\n", (int)a, &a);
}
[tmp]$ ./a.out
0xfefb6764
0 (nil)
0 0xfefb6764
[tmp]$

When passed to a function with variable arguments, float arguments are
promoted to double. A double is usually 64 bits, and the %x format
uses only 32 of these, leaving the remaining 32 for the %p.

--
Måns Rullgård
mru@xxxxxxxxxxxxx
.



Relevant Pages

  • Re: libc/printf bug
    ... I don't see that the cast should have any effect at all ... How would you print a float in hex? ... So seems printf gets confused because you present it with a float when i t wants an integer. ... This behavior I have seen in other cases where wrong or missing arguments were presented too. ...
    (comp.os.linux.development.apps)
  • Re: libc/printf bug
    ... or the author is blind as a bat. ... Am I missing ... I don't see that the cast should have any effect at all ... How would you print a float in hex? ...
    (comp.os.linux.development.apps)
  • Re: problem incrementing my variable
    ... Isn't that taken care of by default argument promotion? ... just before calling printf, for example. ... default argument promotions, from float to double, which matches the ... If he's told it to expect a double by using %f, then he can give a double, or a float, or something cast to a double, or something cast to a float, or any other expression which results in a float or a double. ...
    (comp.lang.c)
  • Re: Floating Point Approximations.
    ... (chosen as 5 decimal places based on the useful precision of the float ... int main{ ... printf; ... point for using, in certain circumstances, BCD math instead of binary ...
    (comp.databases.theory)
  • Re: internals of typecasting
    ... > object of type int is applied an cast of float the result ... > would be of type float. ... casting from floating-point numbers to integers. ...
    (comp.lang.c)