* 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> 2013-10-19 Vladimir Serbinenko <phcoder@gmail.com>
* grub-core/tests/video_checksum.c (grub_video_capture_write_bmp): * 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) while ((c = *fmt++) != 0)
{ {
char tmp[32]; char tmp[32];
char *p;
unsigned int format1 = 0; unsigned int format1 = 0;
unsigned int format2 = ~ 0U; unsigned int format2 = ~ 0U;
char zerofill = ' '; char zerofill = ' ';
@ -854,38 +853,20 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
fmt++; fmt++;
} }
p = (char *) fmt;
/* Read formatting parameters. */ /* Read formatting parameters. */
while (*p && grub_isdigit (*p)) if (grub_isdigit (*fmt))
p++;
if (p > fmt)
{ {
char s[p - fmt + 1]; if (fmt[0] == '0')
grub_strncpy (s, fmt, p - fmt);
s[p - fmt] = 0;
if (s[0] == '0')
zerofill = '0'; zerofill = '0';
format1 = grub_strtoul (s, 0, 10); format1 = grub_strtoul (fmt, (char **) &fmt, 10);
fmt = p;
} }
if (*p && *p == '.') if (*fmt == '.')
{ fmt++;
p++;
fmt++; if (grub_isdigit (*fmt))
while (*p && grub_isdigit (*p)) format2 = grub_strtoul (fmt, (char **) &fmt, 10);
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 (*fmt == '$') if (*fmt == '$')
{ {
curn = format1 - 1; curn = format1 - 1;
@ -1003,25 +984,23 @@ grub_vsnprintf_real (char *str, grub_size_t max_len, const char *fmt0, va_list a
break; break;
case 's': case 's':
p = args[curn].p; {
if (p) grub_size_t len = 0;
{ const char *p = args[curn].p ? : "(null)";
grub_size_t len = 0;
while (len < format2 && p[len])
len++;
if (!rightfill && len < format1) while (len < format2 && p[len])
write_fill (zerofill, format1 - len); len++;
grub_size_t i; if (!rightfill && len < format1)
for (i = 0; i < len; i++) write_fill (zerofill, format1 - len);
write_char (*p++);
if (rightfill && len < format1) grub_size_t i;
write_fill (zerofill, format1 - len); for (i = 0; i < len; i++)
} write_char (*p++);
else
write_str ("(null)"); if (rightfill && len < format1)
write_fill (zerofill, format1 - len);
}
break; break;

View file

@ -28,6 +28,19 @@ printf_test (void)
{ {
char real[512]; char real[512];
char expected[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); grub_snprintf (real, sizeof (real), "%d%%", 10);
snprintf (expected, sizeof (expected), "%d%%", 10); snprintf (expected, sizeof (expected), "%d%%", 10);