From: Wolfgang Denk Date: Mon, 26 Jan 2009 23:30:36 +0100 Subject: [U-Boot] sprintf side effect, a bug? In-Reply-To: <200901261453.10434.e.robertson.svg@gmail.com> References: <200901261453.10434.e.robertson.svg@gmail.com> Message-ID: <20090126223036.1DD27832E416@gemini.denx.de> List-Id: MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit To: u-boot@lists.denx.de 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