Skip to content

Commit 9e0e6d8

Browse files
committed
vsprintf: fix calling convention for format_decode()
Every single caller wants to know what the next format location is, but instead the function returned the length of the processed part and so every single return statement in the format_decode() function was instead subtracting the start of the format string. The callers that that did want to know the length (in addition to the end of the format processing) already had to save off the start of the format string anyway. So this was all just doing extra processing both on the caller and callee sides. Just change the calling convention to return the end of the format processing, making everything simpler (and preparing for yet more simplification to come). Signed-off-by: Linus Torvalds <[email protected]>
1 parent 03d2394 commit 9e0e6d8

File tree

1 file changed

+17
-20
lines changed

1 file changed

+17
-20
lines changed

lib/vsprintf.c

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -2546,7 +2546,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
25462546
* @qualifier: qualifier of a number (long, size_t, ...)
25472547
*/
25482548
static noinline_for_stack
2549-
int format_decode(const char *fmt, struct printf_spec *spec)
2549+
const char *format_decode(const char *fmt, struct printf_spec *spec)
25502550
{
25512551
const char *start = fmt;
25522552
char qualifier;
@@ -2580,7 +2580,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
25802580

25812581
/* Return the current non-format string */
25822582
if (fmt != start || !*fmt)
2583-
return fmt - start;
2583+
return fmt;
25842584

25852585
/* Process flags */
25862586
spec->flags = 0;
@@ -2611,7 +2611,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
26112611
else if (*fmt == '*') {
26122612
/* it's the next argument */
26132613
spec->type = FORMAT_TYPE_WIDTH;
2614-
return ++fmt - start;
2614+
return ++fmt;
26152615
}
26162616

26172617
precision:
@@ -2626,7 +2626,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
26262626
} else if (*fmt == '*') {
26272627
/* it's the next argument */
26282628
spec->type = FORMAT_TYPE_PRECISION;
2629-
return ++fmt - start;
2629+
return ++fmt;
26302630
}
26312631
}
26322632

@@ -2652,19 +2652,19 @@ int format_decode(const char *fmt, struct printf_spec *spec)
26522652
switch (*fmt) {
26532653
case 'c':
26542654
spec->type = FORMAT_TYPE_CHAR;
2655-
return ++fmt - start;
2655+
return ++fmt;
26562656

26572657
case 's':
26582658
spec->type = FORMAT_TYPE_STR;
2659-
return ++fmt - start;
2659+
return ++fmt;
26602660

26612661
case 'p':
26622662
spec->type = FORMAT_TYPE_PTR;
2663-
return ++fmt - start;
2663+
return ++fmt;
26642664

26652665
case '%':
26662666
spec->type = FORMAT_TYPE_PERCENT_CHAR;
2667-
return ++fmt - start;
2667+
return ++fmt;
26682668

26692669
/* integer number formats - set up the flags and "break" */
26702670
case 'o':
@@ -2697,7 +2697,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
26972697
default:
26982698
WARN_ONCE(1, "Please remove unsupported %%%c in format string\n", *fmt);
26992699
spec->type = FORMAT_TYPE_INVALID;
2700-
return fmt - start;
2700+
return fmt;
27012701
}
27022702

27032703
if (qualifier == 'L')
@@ -2716,7 +2716,7 @@ int format_decode(const char *fmt, struct printf_spec *spec)
27162716
spec->type = FORMAT_TYPE_SIZE(int);
27172717
}
27182718

2719-
return ++fmt - start;
2719+
return ++fmt;
27202720
}
27212721

27222722
static void
@@ -2803,14 +2803,14 @@ int vsnprintf(char *buf, size_t size, const char *fmt, va_list args)
28032803

28042804
while (*fmt) {
28052805
const char *old_fmt = fmt;
2806-
int read = format_decode(fmt, &spec);
28072806

2808-
fmt += read;
2807+
fmt = format_decode(fmt, &spec);
28092808

28102809
switch (spec.type) {
28112810
case FORMAT_TYPE_NONE: {
2812-
int copy = read;
2811+
int read = fmt - old_fmt;
28132812
if (str < end) {
2813+
int copy = read;
28142814
if (copy > end - str)
28152815
copy = end - str;
28162816
memcpy(str, old_fmt, copy);
@@ -3089,9 +3089,7 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt, va_list args)
30893089
})
30903090

30913091
while (*fmt) {
3092-
int read = format_decode(fmt, &spec);
3093-
3094-
fmt += read;
3092+
fmt = format_decode(fmt, &spec);
30953093

30963094
switch (spec.type) {
30973095
case FORMAT_TYPE_NONE:
@@ -3233,15 +3231,14 @@ int bstr_printf(char *buf, size_t size, const char *fmt, const u32 *bin_buf)
32333231

32343232
while (*fmt) {
32353233
const char *old_fmt = fmt;
3236-
int read = format_decode(fmt, &spec);
32373234
unsigned long long num;
32383235

3239-
fmt += read;
3240-
3236+
fmt = format_decode(fmt, &spec);
32413237
switch (spec.type) {
32423238
case FORMAT_TYPE_NONE: {
3243-
int copy = read;
3239+
int read = fmt - old_fmt;
32443240
if (str < end) {
3241+
int copy = read;
32453242
if (copy > end - str)
32463243
copy = end - str;
32473244
memcpy(str, old_fmt, copy);

0 commit comments

Comments
 (0)