2009-01-22 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 ().
This commit is contained in:
chrfranke 2009-01-22 20:27:52 +00:00
parent 3138b44c90
commit d31c24f1e0
2 changed files with 36 additions and 21 deletions

View file

@ -1,3 +1,10 @@
2009-01-22 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 ().
2009-01-22 Christian Franke <franke@computer.org> 2009-01-22 Christian Franke <franke@computer.org>
* disk/ata.c (grub_ata_wait_status): Replace by ... * disk/ata.c (grub_ata_wait_status): Replace by ...

View file

@ -700,7 +700,7 @@ grub_vsprintf (char *str, const char *fmt, va_list args)
char tmp[32]; char tmp[32];
char *p; char *p;
unsigned int format1 = 0; unsigned int format1 = 0;
unsigned int format2 = 3; unsigned int format2 = ~ 0U;
char zerofill = ' '; char zerofill = ' ';
int rightfill = 0; int rightfill = 0;
int n; int n;
@ -727,20 +727,22 @@ grub_vsprintf (char *str, const char *fmt, va_list args)
zerofill = '0'; zerofill = '0';
format1 = grub_strtoul (s, 0, 10); format1 = grub_strtoul (s, 0, 10);
fmt = p; fmt = p;
if (*p && *p == '.') }
if (*p && *p == '.')
{
p++;
fmt++;
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
{ {
p++; char fstr[p - fmt + 1];
fmt++; grub_strncpy (fstr, fmt, p - fmt);
while (*p && grub_isdigit (*p)) fstr[p - fmt] = 0;
p++; format2 = grub_strtoul (fstr, 0, 10);
fmt = p;
if (p > fmt)
{
char fstr[p - fmt];
grub_strncpy (fstr, fmt, p - fmt);
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 *); p = va_arg (args, char *);
if (p) if (p)
{ {
if (!rightfill && grub_strlen (p) < format1) grub_size_t len = 0;
write_fill (zerofill, format1 - grub_strlen (p)); while (len < format2 && p[len])
len++;
write_str (p);
if (!rightfill && len < format1)
if (rightfill && grub_strlen (p) < format1) write_fill (zerofill, format1 - len);
write_fill (zerofill, format1 - grub_strlen (p));
grub_size_t i;
for (i = 0; i < len; i++)
write_char (*p++);
if (rightfill && len < format1)
write_fill (zerofill, format1 - len);
} }
else else
write_str ("(null)"); write_str ("(null)");