* grub-core/kern/misc.c (grub_vsnprintf_real): Fix formatting of

"(null)" string.
	Simplify expressions to save around 256 bytes in kernel.img.
	* tests/printf_unit_test.c (printf_test): Add "(null)" tests.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-10-19 23:52:09 +02:00
parent 99519c154c
commit bbd2b5396b
3 changed files with 42 additions and 43 deletions

View file

@ -1,3 +1,10 @@
2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/kern/misc.c (grub_vsnprintf_real): Fix formatting of
"(null)" string.
Simplify expressions to save around 256 bytes in kernel.img.
* tests/printf_unit_test.c (printf_test): Add "(null)" tests.
2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/tests/video_checksum.c (grub_video_capture_write_bmp):

View file

@ -828,7 +828,6 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
while ((c = *fmt++) != 0)
{
char tmp[32];
char *p;
unsigned int format1 = 0;
unsigned int format2 = ~ 0U;
char zerofill = ' ';
@ -854,38 +853,20 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
fmt++;
}
p = (char *) fmt;
/* Read formatting parameters. */
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
if (grub_isdigit (*fmt))
{
char s[p - fmt + 1];
grub_strncpy (s, fmt, p - fmt);
s[p - fmt] = 0;
if (s[0] == '0')
if (fmt[0] == '0')
zerofill = '0';
format1 = grub_strtoul (s, 0, 10);
fmt = p;
format1 = grub_strtoul (fmt, (char **) &fmt, 10);
}
if (*p && *p == '.')
{
p++;
if (*fmt == '.')
fmt++;
while (*p && grub_isdigit (*p))
p++;
if (p > fmt)
{
char fstr[p - fmt + 1];
grub_strncpy (fstr, fmt, p - fmt);
fstr[p - fmt] = 0;
format2 = grub_strtoul (fstr, 0, 10);
fmt = p;
}
}
if (grub_isdigit (*fmt))
format2 = grub_strtoul (fmt, (char **) &fmt, 10);
if (*fmt == '$')
{
curn = format1 - 1;
@ -1003,10 +984,10 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
break;
case 's':
p = args[curn].p;
if (p)
{
grub_size_t len = 0;
const char *p = args[curn].p ? : "(null)";
while (len < format2 && p[len])
len++;
@ -1020,8 +1001,6 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
if (rightfill && len < format1)
write_fill (zerofill, format1 - len);
}
else
write_str ("(null)");
break;

View file

@ -28,6 +28,19 @@ printf_test (void)
{
char real[512];
char expected[512];
char *null = NULL;
grub_snprintf (real, sizeof (real), "%s", null);
snprintf (expected, sizeof (expected), "%s", null);
grub_test_assert (strcmp (real, expected) == 0, MSG);
grub_snprintf (real, sizeof (real), "%10s", null);
snprintf (expected, sizeof (expected), "%10s", null);
grub_test_assert (strcmp (real, expected) == 0, MSG);
grub_snprintf (real, sizeof (real), "%-10s", null);
snprintf (expected, sizeof (expected), "%-10s", null);
grub_test_assert (strcmp (real, expected) == 0, MSG);
grub_snprintf (real, sizeof (real), "%d%%", 10);
snprintf (expected, sizeof (expected), "%d%%", 10);