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

E Robertson e.robertson.svg at gmail.com
Tue Jan 27 21:57:25 CET 2009


On Tuesday 27 January 2009 02:31:52 pm Scott Wood wrote:
> E Robertson wrote:
> > I did the following:
> >     memcpy (gd->bd->bi_enetaddr, MACAddress,
> > sizeof(gd->bd->bi_enetaddr)); memcpy (dupMACAddress, MACAddress,
> > sizeof(dupMACAddress));
> >
> >     printf("Before %02X:%02X:%02X:%02X:%02X:%02X\n",dupMACAddress[0],
> > dupMACAddress[1],
> > dupMACAddress[2], dupMACAddress[3],
> > dupMACAddress[4], dupMACAddress[5]);
> >
> >     sprintf(env_ethaddr,"%02X:%02X:%02X:%02X:%02X:%02X",dupMACAddress[0],
> > dupMACAddress[1],
> > dupMACAddress[2], dupMACAddress[3],
> > dupMACAddress[4], dupMACAddress[5]);
> >     printf("After %02X:%02X:%02X:%02X:%02X:%02X\n",dupMACAddress[0],
> > dupMACAddress[1],
> > dupMACAddress[2], dupMACAddress[3],
> > dupMACAddress[4], dupMACAddress[5]);
> >      printf("ethaddr
> > %02X:%02X:%02X:%02X:%02X:%02X\n",gd->bd->bi_enetaddr[0],
> > gd->bd->bi_enetaddr[1],
> > gd->bd->bi_enetaddr[2], gd->bd->bi_enetaddr[3],
> > gd->bd->bi_enetaddr[4], gd->bd->bi_enetaddr[5]);
> >
> >
> > and I got this:
> >
> > Before 00:04:A6:00:6A:E6
> > After 36:00:A6:00:6A:E6
> > ethaddr 00:04:A6:00:6A:E6
> > Alse env_ethaddr has the correct correct.
>
> Looks like you're overflowing a buffer -- 0x36 0x00 are the last two
> bytes of the sprintf output.  Where are env_ethaddr and dupMACaddress
> defined?
>
Ah!! Yes, big user error! I forgot about the string termination.

Thanks.


> -Scott


-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 197 bytes
Desc: This is a digitally signed message part.
Url : http://lists.denx.de/pipermail/u-boot/attachments/20090127/01ef3c71/attachment.pgp 


More information about the U-Boot mailing list