Skip to content

Commit

Permalink
efi/printf: Factor out flags parsing and handle '%' earlier
Browse files Browse the repository at this point in the history
Move flags parsing code out into a helper function.

The '%%' case can be handled up front: it is not allowed to have flags,
width etc.

Signed-off-by: Arvind Sankar <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Ard Biesheuvel <[email protected]>
  • Loading branch information
nivedita76 authored and ardbiesheuvel committed May 19, 2020
1 parent ce5e3f9 commit 3b83509
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions drivers/firmware/efi/libstub/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,35 @@ static char *number(char *str, long long num, int base, int size, int precision,
return str;
}

static
int get_flags(const char **fmt)
{
int flags = 0;

do {
switch (**fmt) {
case '-':
flags |= LEFT;
break;
case '+':
flags |= PLUS;
break;
case ' ':
flags |= SPACE;
break;
case '#':
flags |= SPECIAL;
break;
case '0':
flags |= ZEROPAD;
break;
default:
return flags;
}
++(*fmt);
} while (1);
}

int vsprintf(char *buf, const char *fmt, va_list args)
{
int len;
Expand All @@ -218,32 +247,13 @@ int vsprintf(char *buf, const char *fmt, va_list args)
int qualifier; /* 'h', 'hh', 'l' or 'll' for integer fields */

for (str = buf; *fmt; ++fmt) {
if (*fmt != '%') {
if (*fmt != '%' || *++fmt == '%') {
*str++ = *fmt;
continue;
}

/* process flags */
flags = 0;
repeat:
++fmt; /* this also skips first '%' */
switch (*fmt) {
case '-':
flags |= LEFT;
goto repeat;
case '+':
flags |= PLUS;
goto repeat;
case ' ':
flags |= SPACE;
goto repeat;
case '#':
flags |= SPECIAL;
goto repeat;
case '0':
flags |= ZEROPAD;
goto repeat;
}
flags = get_flags(&fmt);

/* get field width */
field_width = -1;
Expand Down Expand Up @@ -321,10 +331,6 @@ int vsprintf(char *buf, const char *fmt, va_list args)
field_width, precision, flags);
continue;

case '%':
*str++ = '%';
continue;

/* integer number formats - set up the flags and "break" */
case 'o':
base = 8;
Expand Down

0 comments on commit 3b83509

Please sign in to comment.