Re: [RESEND PATCH v3 1/1] lib/vsprintf: Add support for printing V4L2 and DRM fourccs

From: Joe Perches
Date: Mon Apr 27 2020 - 12:02:40 EST


On Mon, 2020-04-27 at 17:53 +0300, Sakari Ailus wrote:
> Add a printk modifier %p4cc (for pixel format) for printing V4L2 and DRM
> pixel formats denoted by fourccs. The fourcc encoding is the same for both
> so the same implementation can be used.
[]
> - Added WARN_ON_ONCE() sanity checks. Comments on these are welcome; I'd
> expect them mostly be covered by the tests.

All the WARN_ON_ONCE uses are not necessary.

> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
[]
> @@ -1721,6 +1721,89 @@ char *netdev_bits(char *buf, char *end, const void *addr,
> return special_hex_number(buf, end, num, size);
> }
>
> +static noinline_for_stack
> +char *fourcc_string(char *buf, char *end, const u32 *__fourcc,
> + struct printf_spec spec, const char *fmt)

There's no reason to use __ prefixed argument names here.

> +{
> +#define FOURCC_HEX_CHAR_STR "(xx)"
> +#define FOURCC_BIG_ENDIAN_STR " big-endian"
> +#define FOURCC_LITTLE_ENDIAN_STR " little-endian"

I don't believe used-once macro defines are useful.

> +#define FOURCC_HEX_NUMBER " (0x01234567)"

> +#define FOURCC_STRING_MAX \
> + FOURCC_HEX_CHAR_STR FOURCC_HEX_CHAR_STR FOURCC_HEX_CHAR_STR \
> + FOURCC_HEX_CHAR_STR FOURCC_LITTLE_ENDIAN_STR FOURCC_HEX_NUMBER

This is very difficult to read and is size dependent on
the size of little-endian > big_endian

I'd write it out

FOURCC_STRING_MAX sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)")

and then not have the define at all but use
the written out string in the declaration.

> + struct printf_spec my_spec = {
> + .type = FORMAT_TYPE_UINT,
> + .field_width = 2,
> + .flags = SMALL,
> + .base = 16,
> + .precision = -1,
> + };

my_spec isn't usefully named, likely not necessary at all,
and if really necessary, it should be static const

> + char __s[sizeof(FOURCC_STRING_MAX)];

I'd declare the buffer

char fourcc[sizeof("(xx)(xx)(xx)(xx) little-endian (0x01234567)"];

like all the other specialty functions do.

> + char *s = __s;

There's no reason to use __ prefixed variable names here either.
All the other specialty function use:

char *p = <output_buffer_name>;

> + unsigned int i;

just int i; is typical

> + /*
> + * The 31st bit defines the endianness of the data, so save its printing
> + * for later.
> + */
> + u32 fourcc = *__fourcc & ~BIT(31);

bool be = fourcc & BIT(31);

> + int ret;
> +
> + if (check_pointer(&buf, end, __fourcc, spec))
> + return buf;
> +
> + if (fmt[1] != 'c' || fmt[2] != 'c')
> + return error_string(buf, end, "(%p4?)", spec);
> +
> + for (i = 0; i < sizeof(fourcc); i++, fourcc >>= 8) {
> + unsigned char c = fourcc >> (i*8);

Please remove the fourcc >>= 8 from the loop and use

unsigned char c = fourcc >> (i*8);

If I understand this correctly, I think this is simpler:

if (isascii(c) && isprint(c)) {
*s++ = c;
} else {
*s++ = '(';
s = hex_byte_pack(s, c);
*s++ = ')';
}

> +
> + /* Weed out spaces */
> + if (c == ' ')
> + continue;
> +
> + /* Print non-control characters as-is */
> + if (c > ' ') {
> + *s = c;
> + s++;
> + continue;
> + }
> +
> + if (WARN_ON_ONCE(sizeof(__s) <
> + (s - __s) + sizeof(FOURCC_HEX_CHAR_STR)))
> + break;
> +
> + *s = '(';
> + s++;
> + s = number(s, s + 2, c, my_spec);
> + *s = ')';
> + s++;
> + }
> +
> + ret = strscpy(s, *__fourcc & BIT(31) ? FOURCC_BIG_ENDIAN_STR
> + : FOURCC_LITTLE_ENDIAN_STR,
> + sizeof(__s) - (s - __s));

If you size the initial buffer correctly, strscpy is unnecessary.

strcpy(s, <bit31> ? "big endian" : "little endian");
s += strlen(s);

> + if (!WARN_ON_ONCE(ret < 0))
> + s += ret;
> +
> + if (!WARN_ON_ONCE(sizeof(__s) <
> + (s - __s) + sizeof(FOURCC_HEX_NUMBER))) {
> + *s = ' ';
> + s++;
> + *s = '(';
> + s++;

*s++ = ' ';
*s++ = '(';

+ /* subtract parentheses and the space from the size */
> + special_hex_number(s, s + sizeof(FOURCC_HEX_NUMBER) - 3,
> + *__fourcc, sizeof(u32));
> + s += sizeof(u32) * 2 + 2 /* 0x */;
> + *s = ')';
> + s++;
*s++ = ')';

> + *s = '\0';
> + }
> +
> + return string(buf, end, __s, spec);
> +}
>