sprintf bug on Linux
Michael Meissner
meissner@cygnus.com
Fri Apr 28 10:11:00 GMT 2000
On Fri, Apr 28, 2000 at 09:55:09AM -0700, Brian Keefe wrote:
>
>
> If I use
> char b[2]
> below rather than
> char b[3],
> the value of len is set to zero.
>
> This does not happen on Solaris with gcc, nor on NT with MSVC.
This is a bug in your program, not in GCC. The function sprintf (b, "%02x", f)
writes 3 bytes (the two hex digits followed by a NULL byte). It just so
happens that for your particular function, len was the next item in memory
after b, and when sprintf put the null byte in b[2], it overwrote len (which
given the x86 is little endian, is the byte where the value is stored). If you
had optimized, it likely would have modified something else, since the compiler
will know that len is always constant, and not assign it a stack location.
--
Michael Meissner, Cygnus Solutions, a Red Hat company.
PMB 198, 174 Littleton Road #3, Westford, Massachusetts 01886, USA
Work: meissner@redhat.com phone: +1 978-486-9304
Non-work: meissner@spectacle-pond.org fax: +1 978-692-4482
More information about the Gcc-bugs
mailing list