Skip to content

Commit 376c18f

Browse files
andy-shevpmladek
authored andcommitted
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>
1 parent 372a12b commit 376c18f

1 file changed

Lines changed: 10 additions & 16 deletions

File tree

lib/vsprintf.c

Lines changed: 10 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -582,17 +582,18 @@ char *number(char *buf, char *end, unsigned long long num,
582582
return buf;
583583
}
584584

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+
585593
static noinline_for_stack
586594
char *special_hex_number(char *buf, char *end, unsigned long long num, int size)
587595
{
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));
596597
}
597598

598599
static void move_right(char *buf, char *end, unsigned len, unsigned spaces)
@@ -1164,18 +1165,11 @@ char *range_string(char *buf, char *end, const struct range *range,
11641165
char sym[sizeof("[range 0x0123456789abcdef-0x0123456789abcdef]")];
11651166
char *p = sym, *pend = sym + sizeof(sym);
11661167

1167-
struct printf_spec range_spec = {
1168-
.field_width = 2 + 2 * sizeof(range->start), /* 0x + 2 * 8 */
1169-
.flags = SPECIAL | SMALL | ZEROPAD,
1170-
.base = 16,
1171-
.precision = -1,
1172-
};
1173-
11741168
if (check_pointer(&buf, end, range, spec))
11751169
return buf;
11761170

11771171
p = string_nocheck(p, pend, "[range ", default_str_spec);
1178-
p = hex_range(p, pend, range->start, range->end, range_spec);
1172+
p = hex_range(p, pend, range->start, range->end, special_hex_spec(sizeof(range->start)));
11791173
*p++ = ']';
11801174
*p = '\0';
11811175

0 commit comments

Comments
 (0)