kern/85520: [patch] [trivial] Fix bug in kernel printf(9) formatting

Oliver Fromme olli at secnetix.de
Wed Aug 31 12:40:14 GMT 2005


>Number:         85520
>Category:       kern
>Synopsis:       [patch] [trivial] Fix bug in kernel printf(9) formatting
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 31 12:40:13 GMT 2005
>Closed-Date:
>Last-Modified:
>Originator:     Oliver Fromme
>Release:        HEAD, RELENG_6 (6.0-BETA3), RELENG_5, RELENG_4
>Organization:
secnetix GmbH & Co. KG, Munich, Germany
>Environment:

   This patch applies to RELENG_4 and all later versions
   up to HEAD, including 6.0-BETA3.

>Description:

   The left-padding (when ladjust is not set) does not work
   correctly in the kernel's printf(9) implementation which
   can be found in src/sys/kern/subr_prf.c.

   printf("%5d", -42);    -->   "  -42"   (correct)
   printf("%#5x", 12);    -->   "  0xc"   (correct)
   printf("%05d", -42);   -->   "00-42"   (should be "-0042")
   printf("%#05x", 12);   -->   "000xc"   (should be "0x00c")

   Obviously, when zero-padding ('0' flag) is requested, the
   padding should occur _after_ any prefixes (sign, "0x").
   Otherwise (space-padding), it should occur _before_ any
   prefixes.  In the current (broken) implementation, the
   padding always happens first.  The patch below fixes that
   behaviour.

   By the way, there are more bugs which this patch doesn't
   address yet:  The '+' and ' ' (space) flags aren't handled
   correctly, the precision is not used for numeric conversions
   (in which case the '0' flag must be ignored), and possibly
   others.  I'll try to fix those later.

>How-To-Repeat:

   Insert a printf() in your kernel somewhere which prints a
   negative number (or a hex number with '#') with zero-padding.

>Fix:

--- src/sys/kern/subr_prf.c.orig	Tue Jun  7 00:18:32 2005
+++ src/sys/kern/subr_prf.c	Wed Aug 31 09:00:41 2005
@@ -755,7 +755,8 @@
 			if (neg)
 				tmp++;
 
-			if (!ladjust && width && (width -= tmp) > 0)
+			if (!ladjust && padc != '0' && width
+			    && (width -= tmp) > 0)
 				while (width--)
 					PCHAR(padc);
 			if (neg)
@@ -768,6 +769,9 @@
 					PCHAR('x');
 				}
 			}
+			if (!ladjust && width && (width -= tmp) > 0)
+				while (width--)
+					PCHAR(padc);
 
 			while (*p)
 				PCHAR(*p--);
>Release-Note:
>Audit-Trail:
>Unformatted:


More information about the freebsd-bugs mailing list