[En-Nut-Discussion] [PATCH] Bug in putf

Andre Albsmeier andre.albsmeier at siemens.com
Wed Aug 27 12:45:04 CEST 2003


On Mon, 07-Apr-2003 at 22:17:56 +1200, Ralph Mason wrote:
> Hi,
> 
> I noticed a bug in putf
> 
> When you print an unsigned value that is greater than the max signed value
> for the type putf has a problem.  The code putf sign extends the unsigned
> value into a long (because it treats the unsigned value as signed when it
> copies to ulval)
> 
> thus
> 
> buff[32];
> sprintf(buff,"%u",0xffff);
> 
> Yields a very unexpected result (0xffffffff formatted as an unsigned long).
> 

Noticed the same today :-)

--- crt/putf.c.ORI	Wed Aug 27 11:17:29 2003
+++ crt/putf.c	Wed Aug 27 12:31:13 2003
@@ -238,7 +238,13 @@
             sign = 0;
         case 'd':
         case 'i':
-            ulval = (flags & LONGINT) ? va_arg(ap, long) : va_arg(ap, int);
+	    if( flags & LONGINT )
+		ulval = va_arg(ap, long);
+	    else if (ch == 'u')
+		ulval = va_arg(ap, unsigned int);
+	    else
+		ulval = va_arg(ap, int);
+
             if (ch != 'u' && (long) ulval < 0) {
                 ulval = (u_long) (-((long) ulval));
                 sign = '-';


	-Andre



More information about the En-Nut-Discussion mailing list