Skip to content

Commit

Permalink
vsprintf: correctly handle width when '#' flag used in %#p format
Browse files Browse the repository at this point in the history
The '%p' output of the kernel's vsprintf() uses spec.field_width to
determine how many digits to output based on 2 * sizeof(void*) so that all
digits of a pointer are shown.  ie.  a pointer will be output as
"001A2B3C" instead of "1A2B3C".  However, if the '#' flag is used in the
format (%#p), then the code doesn't take into account the width of the
'0x' prefix and will end up outputing "0x1A2B3C" instead of "0x001A2B3C".

This patch reworks the "pointer()" format hook to include 2 characters for
the '0x' prefix if the '#' flag is included.

[[email protected]: checkpatch fixes]
Signed-off-by: Grant Likely <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
glikely authored and torvalds committed Jun 1, 2012
1 parent d84970b commit 725fe00
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions lib/vsprintf.c
Original file line number Diff line number Diff line change
Expand Up @@ -870,13 +870,15 @@ static noinline_for_stack
char *pointer(const char *fmt, char *buf, char *end, void *ptr,
struct printf_spec spec)
{
int default_width = 2 * sizeof(void *) + (spec.flags & SPECIAL ? 2 : 0);

if (!ptr && *fmt != 'K') {
/*
* Print (null) with the same width as a pointer so it makes
* tabular output look nice.
*/
if (spec.field_width == -1)
spec.field_width = 2 * sizeof(void *);
spec.field_width = default_width;
return string(buf, end, "(null)", spec);
}

Expand Down Expand Up @@ -931,7 +933,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
*/
if (in_irq() || in_serving_softirq() || in_nmi()) {
if (spec.field_width == -1)
spec.field_width = 2 * sizeof(void *);
spec.field_width = default_width;
return string(buf, end, "pK-error", spec);
}
if (!((kptr_restrict == 0) ||
Expand All @@ -948,7 +950,7 @@ char *pointer(const char *fmt, char *buf, char *end, void *ptr,
}
spec.flags |= SMALL;
if (spec.field_width == -1) {
spec.field_width = 2 * sizeof(void *);
spec.field_width = default_width;
spec.flags |= ZEROPAD;
}
spec.base = 16;
Expand Down

0 comments on commit 725fe00

Please sign in to comment.