public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
* [U-Boot] sprintf side effect, a bug?
@ 2009-01-26 20:53 E Robertson
  2009-01-26 22:30 ` Wolfgang Denk
  0 siblings, 1 reply; 5+ messages in thread
From: E Robertson @ 2009-01-26 20:53 UTC (permalink / raw)
  To: u-boot

Hi,
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.

-------------- 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/20090126/68e825fd/attachment.pgp 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] sprintf side effect, a bug?
  2009-01-26 20:53 [U-Boot] sprintf side effect, a bug? E Robertson
@ 2009-01-26 22:30 ` Wolfgang Denk
  2009-01-27 20:19   ` E Robertson
  0 siblings, 1 reply; 5+ messages in thread
From: Wolfgang Denk @ 2009-01-26 22:30 UTC (permalink / raw)
  To: u-boot

Dear E Robertson,

In message <200901261453.10434.e.robertson.svg@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@denx.de
"Anyone attempting to generate random numbers by deterministic  means
is, of course, living in a state of sin."          - John Von Neumann

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] sprintf side effect, a bug?
  2009-01-26 22:30 ` Wolfgang Denk
@ 2009-01-27 20:19   ` E Robertson
  2009-01-27 20:31     ` Scott Wood
  0 siblings, 1 reply; 5+ messages in thread
From: E Robertson @ 2009-01-27 20:19 UTC (permalink / raw)
  To: u-boot

On Monday 26 January 2009 04:30:36 pm Wolfgang Denk wrote:
> Dear E Robertson,
>
> In message <200901261453.10434.e.robertson.svg@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"
>

Hi,

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.

I make a duplicate before did the sprintf.
I'm also on v 1.3.4 and haven't tried it on the latest git.

>
> Looks sane to me...
>
> Best regards,
>
> Wolfgang Denk


-------------- 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/2a0e2f89/attachment.pgp 

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] sprintf side effect, a bug?
  2009-01-27 20:19   ` E Robertson
@ 2009-01-27 20:31     ` Scott Wood
  2009-01-27 20:57       ` E Robertson
  0 siblings, 1 reply; 5+ messages in thread
From: Scott Wood @ 2009-01-27 20:31 UTC (permalink / raw)
  To: u-boot

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?

-Scott

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [U-Boot] sprintf side effect, a bug?
  2009-01-27 20:31     ` Scott Wood
@ 2009-01-27 20:57       ` E Robertson
  0 siblings, 0 replies; 5+ messages in thread
From: E Robertson @ 2009-01-27 20:57 UTC (permalink / raw)
  To: u-boot

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 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2009-01-27 20:57 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-01-26 20:53 [U-Boot] sprintf side effect, a bug? E Robertson
2009-01-26 22:30 ` Wolfgang Denk
2009-01-27 20:19   ` E Robertson
2009-01-27 20:31     ` Scott Wood
2009-01-27 20:57       ` E Robertson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox