From olli@lurza.secnetix.de  Wed Aug 31 12:34:26 2005
Return-Path: <olli@lurza.secnetix.de>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 9351A16A41F
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 31 Aug 2005 12:34:26 +0000 (GMT)
	(envelope-from olli@lurza.secnetix.de)
Received: from lurza.secnetix.de (lurza.secnetix.de [83.120.8.8])
	by mx1.FreeBSD.org (Postfix) with ESMTP id D482143D48
	for <FreeBSD-gnats-submit@freebsd.org>; Wed, 31 Aug 2005 12:34:25 +0000 (GMT)
	(envelope-from olli@lurza.secnetix.de)
Received: from lurza.secnetix.de (venmbc@localhost [127.0.0.1])
	by lurza.secnetix.de (8.13.1/8.13.1) with ESMTP id j7VCYNqh046381;
	Wed, 31 Aug 2005 14:34:23 +0200 (CEST)
	(envelope-from oliver.fromme@secnetix.de)
Received: (from olli@localhost)
	by lurza.secnetix.de (8.13.1/8.13.1/Submit) id j7VCYNPN046380;
	Wed, 31 Aug 2005 14:34:23 +0200 (CEST)
	(envelope-from olli)
Message-Id: <200508311234.j7VCYNPN046380@lurza.secnetix.de>
Date: Wed, 31 Aug 2005 14:34:23 +0200 (CEST)
From: Oliver Fromme <olli@secnetix.de>
Reply-To: Oliver Fromme <olli@secnetix.de>
To: FreeBSD-gnats-submit@freebsd.org
Cc: Oliver Fromme <olli@secnetix.de>
Subject: [patch] [trivial] Fix bug in kernel printf(9) formatting
X-Send-Pr-Version: 3.113
X-GNATS-Notify:

>Number:         85520
>Category:       kern
>Synopsis:       [patch] [trivial] Fix bug in kernel printf(9) formatting
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    delphij
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Wed Aug 31 12:40:13 GMT 2005
>Closed-Date:    Fri Sep 16 17:01:15 GMT 2005
>Last-Modified:  Fri Sep 16 17:01:15 GMT 2005
>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:
State-Changed-From-To: open->patched 
State-Changed-By: delphij 
State-Changed-When: Sun Sep 4 18:04:13 GMT 2005 
State-Changed-Why:  
Patch committed against -HEAD 


Responsible-Changed-From-To: freebsd-bugs->delphij 
Responsible-Changed-By: delphij 
Responsible-Changed-When: Sun Sep 4 18:04:13 GMT 2005 
Responsible-Changed-Why:  
Handle;  MFC Reminder. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=85520 
State-Changed-From-To: patched->closed 
State-Changed-By: delphij 
State-Changed-When: Fri Sep 16 17:00:42 GMT 2005 
State-Changed-Why:  
MFC'ed to RELENG_6 and RELENG_5.  Thanks for your submission! 

http://www.freebsd.org/cgi/query-pr.cgi?pr=85520 
>Unformatted:
