From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mailman by lists.gnu.org with archive (Exim 4.43) id 1LPiOq-00054R-1i for mharc-grub-devel@gnu.org; Wed, 21 Jan 2009 14:06:04 -0500 Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1LPiOo-000548-Ha for grub-devel@gnu.org; Wed, 21 Jan 2009 14:06:02 -0500 Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1LPiOn-00053n-7C for grub-devel@gnu.org; Wed, 21 Jan 2009 14:06:01 -0500 Received: from [199.232.76.173] (port=33138 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1LPiOn-00053g-11 for grub-devel@gnu.org; Wed, 21 Jan 2009 14:06:01 -0500 Received: from mailout06.t-online.de ([194.25.134.19]:53213) by monty-python.gnu.org with esmtp (Exim 4.60) (envelope-from ) id 1LPiOm-0000p1-D6 for grub-devel@gnu.org; Wed, 21 Jan 2009 14:06:00 -0500 Received: from fwd11.aul.t-online.de by mailout06.sul.t-online.de with smtp id 1LPiOi-0008AD-02; Wed, 21 Jan 2009 20:05:56 +0100 Received: from [10.3.2.2] (EeyzqyZlQhM4YW+FukyveIMoCrOBTT2lqgG0tIE6nDdQjnPhTrUgyubi9tg1zaZZ+t@[217.235.237.41]) by fwd11.aul.t-online.de with esmtp id 1LPiOH-1LVuvQ0; Wed, 21 Jan 2009 20:05:29 +0100 Message-ID: <497771FA.8010200@t-online.de> Date: Wed, 21 Jan 2009 20:05:30 +0100 From: Christian Franke User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.16) Gecko/20080702 SeaMonkey/1.1.11 MIME-Version: 1.0 To: grub-devel@gnu.org Content-Type: multipart/mixed; boundary="------------000302040402080208020008" X-ID: EeyzqyZlQhM4YW+FukyveIMoCrOBTT2lqgG0tIE6nDdQjnPhTrUgyubi9tg1zaZZ+t X-TOI-MSGID: 1dcfca25-69fe-467e-9336-dc91055352c4 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 3) Subject: [PATCH] Add 'precision' to grub_printf %s format X-BeenThere: grub-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list Reply-To: The development of GRUB 2 List-Id: The development of GRUB 2 List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Jan 2009 19:06:02 -0000 This is a multi-part message in MIME format. --------------000302040402080208020008 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit This patch adds 'precision' support to grub_printf '%s' format, e.g.: grub_printf("data=%.20s...\n", data); This feature of standard printf() is useful to limit output length or to print strings which are not 0-terminated. Christian 2009-01-21 Christian Franke * kern/misc.c (grub_vsprintf): Fix size and termination of `format2' (precision) digit string. Allow `.format2' without `format1' (width). Limit input chars for `%s' output to `format2' if specified. This is compatible with standard printf (). --------------000302040402080208020008 Content-Type: text/x-diff; name="grub2-printf-string-precision.patch" Content-Transfer-Encoding: 7bit Content-Disposition: inline; filename="grub2-printf-string-precision.patch" diff --git a/kern/misc.c b/kern/misc.c index 6fbc651..da64eb0 100644 --- a/kern/misc.c +++ b/kern/misc.c @@ -700,7 +700,7 @@ grub_vsprintf (char *str, const char *fmt, va_list args) char tmp[32]; char *p; unsigned int format1 = 0; - unsigned int format2 = 3; + unsigned int format2 = ~ 0U; char zerofill = ' '; int rightfill = 0; int n; @@ -727,20 +727,22 @@ grub_vsprintf (char *str, const char *fmt, va_list args) zerofill = '0'; format1 = grub_strtoul (s, 0, 10); fmt = p; - if (*p && *p == '.') + } + + if (*p && *p == '.') + { + p++; + fmt++; + while (*p && grub_isdigit (*p)) + p++; + + if (p > fmt) { - p++; - fmt++; - while (*p && grub_isdigit (*p)) - p++; - - if (p > fmt) - { - char fstr[p - fmt]; - grub_strncpy (fstr, fmt, p - fmt); - format2 = grub_strtoul (fstr, 0, 10); - fmt = p; - } + char fstr[p - fmt + 1]; + grub_strncpy (fstr, fmt, p - fmt); + fstr[p - fmt] = 0; + format2 = grub_strtoul (fstr, 0, 10); + fmt = p; } } @@ -847,13 +849,19 @@ grub_vsprintf (char *str, const char *fmt, va_list args) p = va_arg (args, char *); if (p) { - if (!rightfill && grub_strlen (p) < format1) - write_fill (zerofill, format1 - grub_strlen (p)); - - write_str (p); - - if (rightfill && grub_strlen (p) < format1) - write_fill (zerofill, format1 - grub_strlen (p)); + grub_size_t len = 0; + while (len < format2 && p[len]) + len++; + + if (!rightfill && len < format1) + write_fill (zerofill, format1 - len); + + grub_size_t i; + for (i = 0; i < len; i++) + write_char (*p++); + + if (rightfill && len < format1) + write_fill (zerofill, format1 - len); } else write_str ("(null)"); --------------000302040402080208020008--