Skip to content

Commit 4c53804

Browse files
committed
vsprintf: don't make the 'binary' version pack small integer arguments
The strange vbin_printf / bstr_printf interface used to save one- and two-byte printf numerical arguments into their packed format. That's more than a bit strange since the argument buffer is supposed to be an array of 'u32' words, and it's also very different from how the source of the data (varargs) work - which always do the normal integer type conversions, so 'char' and 'short' are always passed as int-sized anyway. This odd packing causes extra code complexity, and it really isn't worth it, since the space savings are simply not there: it only happens for formats like '%hd' (short) and '%hhd' (char), which are very rare indeed. In fact, the only other user of this interface seems to be the bpf helper code (bpf_bprintf_prepare()), and Alexei points out that that case doesn't support those truncated integer formatting options at all in the first place. As a result, bpf_bprintf_prepare() doesn't need any changes for this, and TRACE_BPRINT uses 'vbin_printf()' -> 'bstr_printf()' for the formatting and hopefully doesn't expose the odd packing any other way (knock wood). Link: https://lore.kernel.org/all/CAADnVQJy65oOubjxM-378O3wDfhuwg8TGa9hc-cTv6NmmUSykQ@mail.gmail.com/ Signed-off-by: Linus Torvalds <[email protected]>
1 parent 8d4826c commit 4c53804

File tree

1 file changed

+6
-23
lines changed

1 file changed

+6
-23
lines changed

lib/vsprintf.c

Lines changed: 6 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3137,17 +3137,9 @@ int vbin_printf(u32 *bin_buf, size_t size, const char *fmt_str, va_list args)
31373137
break;
31383138

31393139
case FORMAT_STATE_NUM:
3140-
switch (fmt.size) {
3141-
case 8:
3140+
if (fmt.size > sizeof(int)) {
31423141
save_arg(long long);
3143-
break;
3144-
case 1:
3145-
save_arg(char);
3146-
break;
3147-
case 2:
3148-
save_arg(short);
3149-
break;
3150-
default:
3142+
} else {
31513143
save_arg(int);
31523144
}
31533145
}
@@ -3318,23 +3310,14 @@ int bstr_printf(char *buf, size_t size, const char *fmt_str, const u32 *bin_buf)
33183310
goto out;
33193311

33203312
case FORMAT_STATE_NUM:
3321-
switch (fmt.size) {
3322-
case 8:
3313+
if (fmt.size > sizeof(int)) {
33233314
num = get_arg(long long);
3324-
break;
3325-
case 1:
3326-
num = convert_num_spec(get_arg(char), fmt.size, spec);
3327-
break;
3328-
case 2:
3329-
num = convert_num_spec(get_arg(short), fmt.size, spec);
3330-
break;
3331-
default:
3315+
} else {
33323316
num = convert_num_spec(get_arg(int), fmt.size, spec);
3333-
break;
33343317
}
3318+
str = number(str, end, num, spec);
3319+
continue;
33353320
}
3336-
3337-
str = number(str, end, num, spec);
33383321
} /* while(*fmt.str) */
33393322

33403323
out:

0 commit comments

Comments
 (0)