Re: libc/printf bug



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.

2. %x are not for printing floats.

[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]$



--
Nils O. Selåsdal
www.utelsystems.com
.



Relevant Pages

  • Re: nrand48 ?!?!?!?!
    ... You call it with an array ... of 3 unsigned short int values which contain values for storing the ... another different compiler and another different libc: ...
    (comp.lang.c)
  • Re: Confused by segfault with legitimate call to strerror(3) on amd64 / sysctl (3) setting `odd errn
    ... 2- it bombs on amd64 ... The compiler thinks strerror returns an int. ... Standard C Library (libc, -lc) ...
    (freebsd-hackers)
  • Re: Direct Linux syscalls
    ... >>and tell why you cannot use the standard one. ... the static libc takes just those functions you really ... > too, time permitting)...indeed, this HLA compiler offers OOP support (not ... except for things that have to be coded in assembler. ...
    (comp.os.linux.development.apps)
  • Re: Direct Linux syscalls
    ... > and tell why you cannot use the standard one. ... And there are a number of "light libc" variants out there, ... perfect in producing code (indeed, the term is "optimising compiler", NOT ... things out...suppress "libc" including this or that...there's redundency ...
    (comp.os.linux.development.apps)
  • Re: Two copies of resolver routines in libc ?
    ... the resolver functions in getaddrinfo.c has some addition of ... Hide the internal functions from outside of libc. ... +extern int ...
    (freebsd-net)