Skip to content

Commit 3b83509

Browse files
nivedita76ardbiesheuvel
authored andcommitted
efi/printf: Factor out flags parsing and handle '%' earlier
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]>
1 parent ce5e3f9 commit 3b83509

File tree

1 file changed

+31
-25
lines changed

1 file changed

+31
-25
lines changed

drivers/firmware/efi/libstub/vsprintf.c

Lines changed: 31 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,35 @@ static char *number(char *str, long long num, int base, int size, int precision,
202202
return str;
203203
}
204204

205+
static
206+
int get_flags(const char **fmt)
207+
{
208+
int flags = 0;
209+
210+
do {
211+
switch (**fmt) {
212+
case '-':
213+
flags |= LEFT;
214+
break;
215+
case '+':
216+
flags |= PLUS;
217+
break;
218+
case ' ':
219+
flags |= SPACE;
220+
break;
221+
case '#':
222+
flags |= SPECIAL;
223+
break;
224+
case '0':
225+
flags |= ZEROPAD;
226+
break;
227+
default:
228+
return flags;
229+
}
230+
++(*fmt);
231+
} while (1);
232+
}
233+
205234
int vsprintf(char *buf, const char *fmt, va_list args)
206235
{
207236
int len;
@@ -218,32 +247,13 @@ int vsprintf(char *buf, const char *fmt, va_list args)
218247
int qualifier; /* 'h', 'hh', 'l' or 'll' for integer fields */
219248

220249
for (str = buf; *fmt; ++fmt) {
221-
if (*fmt != '%') {
250+
if (*fmt != '%' || *++fmt == '%') {
222251
*str++ = *fmt;
223252
continue;
224253
}
225254

226255
/* process flags */
227-
flags = 0;
228-
repeat:
229-
++fmt; /* this also skips first '%' */
230-
switch (*fmt) {
231-
case '-':
232-
flags |= LEFT;
233-
goto repeat;
234-
case '+':
235-
flags |= PLUS;
236-
goto repeat;
237-
case ' ':
238-
flags |= SPACE;
239-
goto repeat;
240-
case '#':
241-
flags |= SPECIAL;
242-
goto repeat;
243-
case '0':
244-
flags |= ZEROPAD;
245-
goto repeat;
246-
}
256+
flags = get_flags(&fmt);
247257

248258
/* get field width */
249259
field_width = -1;
@@ -321,10 +331,6 @@ int vsprintf(char *buf, const char *fmt, va_list args)
321331
field_width, precision, flags);
322332
continue;
323333

324-
case '%':
325-
*str++ = '%';
326-
continue;
327-
328334
/* integer number formats - set up the flags and "break" */
329335
case 'o':
330336
base = 8;

0 commit comments

Comments
 (0)