Re: libc/printf bug



Micah Cowan <micah@xxxxxxxxxx> writes:

"Nils O. Selåsdal" <NOS@xxxxxxx> writes:

bill pursell wrote:
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?
There are quite a few errors in this code.
libc is not at fault.

1. Forgot to include <stdio.h>, this is important so
you get a proper prototye for printf, else the compiler
assumes all arguments are int - and who knows what happens
when it itsn't so, and the compiler generates wrong code
accroding to what you intended.

This isn't actually true: it will do exactly the same promotions to
printf()s arguments as /already/ occur when calling printf().

The promotions apply when calling a function without a prototype or a
variadic function.

Calling it without a prototype in scope will cause gcc to complain
that the implicit declaration of printf() is incompatible with its
actual prototype, but it apparently still works as expected; however,

That's because gcc knows what some functions are supposed to look
like. This is in no way required by the standard, but is useful
nonetheless.

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



Relevant Pages

  • Re: variable allocated from stack/bss ??
    ... one of which is that printf is not properly prototyped. ... prototype in scope invokes undefined behavior. ... An implementation could use a completely different calling ... than on the stack. ...
    (comp.lang.c)
  • Re: libc/printf bug
    ... a bug in libc, or the author is blind as a bat. ... that printf is mangling the address unless the second argument ... Calling it without a prototype in scope will cause gcc to complain ...
    (comp.os.linux.development.apps)
  • Re: to stdio or not to stdio
    ... It is necessary to include the correct prototype for printf(), ... Where this prototype comes from does not matter. ... > warnings and just use int functionname() vs. int functionname(void) but ...
    (comp.lang.c)
  • Re: Can u tell me the explanation reg this problem
    ... value although it's implicitly declared to return int and there isn't a ... proper prototype of printf in scope), ... the Rationale addresses exactly the case at hand: ...
    (comp.lang.c)
  • Re: Simple allocation of two buffer on AMD64
    ... question - which did not declare printf() - the call to printfin its ... entirety has undefined behaviour. ... # either the prototype ends with an ellipsis... ...
    (comp.lang.c)