From: Christian Franke <Christian.Franke@t-online.de>
To: grub-devel@gnu.org
Subject: [PATCH] Add 'precision' to grub_printf %s format
Date: Wed, 21 Jan 2009 20:05:30 +0100 [thread overview]
Message-ID: <497771FA.8010200@t-online.de> (raw)
[-- Attachment #1: Type: text/plain, Size: 545 bytes --]
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 <franke@computer.org>
* 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 ().
[-- Attachment #2: grub2-printf-string-precision.patch --]
[-- Type: text/x-diff, Size: 1862 bytes --]
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)");
next reply other threads:[~2009-01-21 19:06 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2009-01-21 19:05 Christian Franke [this message]
2009-01-22 20:28 ` [PATCH] Add 'precision' to grub_printf %s format Christian Franke
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=497771FA.8010200@t-online.de \
--to=christian.franke@t-online.de \
--cc=grub-devel@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.