Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lib/vsprintf: Deduplicate special hex number specifier data

Two functions use the same specifier data for the special hex number.
Almost the same as the field width is calculated on the size of the
given type. Due to that, make a compound literal macro in order to
deduplicate the rest.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
Reviewed-by: Petr Mladek <pmladek@suse.com>
Tested-by: Petr Mladek <pmladek@suse.com>
Link: https://patch.msgid.link/20251113150313.3030700-1-andriy.shevchenko@linux.intel.com
Signed-off-by: Petr Mladek <pmladek@suse.com>

authored by

Andy Shevchenko and committed by
Petr Mladek
376c18f3 372a12bd

+10 -16
+10 -16
lib/vsprintf.c
··· 582 582 return buf; 583 583 } 584 584 585 + #define special_hex_spec(size) \ 586 + (struct printf_spec) { \ 587 + .field_width = 2 + 2 * (size), /* 0x + hex */ \ 588 + .flags = SPECIAL | SMALL | ZEROPAD, \ 589 + .base = 16, \ 590 + .precision = -1, \ 591 + } 592 + 585 593 static noinline_for_stack 586 594 char *special_hex_number(char *buf, char *end, unsigned long long num, int size) 587 595 { 588 - struct printf_spec spec; 589 - 590 - spec.field_width = 2 + 2 * size; /* 0x + hex */ 591 - spec.flags = SPECIAL | SMALL | ZEROPAD; 592 - spec.base = 16; 593 - spec.precision = -1; 594 - 595 - return number(buf, end, num, spec); 596 + return number(buf, end, num, special_hex_spec(size)); 596 597 } 597 598 598 599 static void move_right(char *buf, char *end, unsigned len, unsigned spaces) ··· 1165 1164 char sym[sizeof("[range 0x0123456789abcdef-0x0123456789abcdef]")]; 1166 1165 char *p = sym, *pend = sym + sizeof(sym); 1167 1166 1168 - struct printf_spec range_spec = { 1169 - .field_width = 2 + 2 * sizeof(range->start), /* 0x + 2 * 8 */ 1170 - .flags = SPECIAL | SMALL | ZEROPAD, 1171 - .base = 16, 1172 - .precision = -1, 1173 - }; 1174 - 1175 1167 if (check_pointer(&buf, end, range, spec)) 1176 1168 return buf; 1177 1169 1178 1170 p = string_nocheck(p, pend, "[range ", default_str_spec); 1179 - p = hex_range(p, pend, range->start, range->end, range_spec); 1171 + p = hex_range(p, pend, range->start, range->end, special_hex_spec(sizeof(range->start))); 1180 1172 *p++ = ']'; 1181 1173 *p = '\0'; 1182 1174