[U-Boot] sprintf side effect, a bug?

Wolfgang Denk wd at denx.de
Mon Jan 26 23:30:36 CET 2009


Dear E Robertson,

In message <200901261453.10434.e.robertson.svg at gmail.com> you wrote:
>
> It seems that sprintf() is modifying at least two bytes from the source and I 
> have duplicate this with different varibles.
> For instance, I have a mac address defined as unsigned char [6]:
> Doing the following sets the environment variable correctly, however bytes [0] 
> and [1], are modified at the source.
>
> sprintf(env_ethaddr,"%02X:%02X:%02X:%02X:%02X:%02X", MACAddress[0], 
> MACAddress[1],
> MACAddress[2], MACAddress[3],
> MACAddress[4], MACAddress[5]);
>
> Has anyone noticed this before?
> I don't think the machine matters but I am building for an arm at91.

Hmm... I think I wouldbe surprised. What exactly does your test code
look like?

I tried this one:

...
unsigned char buf[128];
unsigned char a[6] = { 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, };
int i;
...
printf ("Before: "); for (i=0; i<6; ++i) printf (" %02X",a[i]); putc ('\n');
sprintf (buf, "%02X:%02X:%02X:%02X:%02X:%02X", a[0], a[1], a[2], a[3], a[4], a[5]);
printf ("After:  "); for (i=0; i<6; ++i) printf (" %02X",a[i]); putc ('\n');
printf ("buf=\"%s\"\n", buf);
...

And this is what I got:

Before:  11 22 33 44 55 66
After:   11 22 33 44 55 66
buf="11:22:33:44:55:66"



Looks sane to me...

Best regards,

Wolfgang Denk

-- 
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
"Anyone attempting to generate random numbers by deterministic  means
is, of course, living in a state of sin."          - John Von Neumann


More information about the U-Boot mailing list