Re: [PATCH v1 1/3] lib/vsprintf: Replace hidden BUILD_BUG_ON() with static_assert()

From: Steven Rostedt
Date: Fri Jul 31 2020 - 10:38:04 EST


On Fri, 31 Jul 2020 15:31:43 +0300
Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx> wrote:

> First of all, there is no compile time check for the SMALL
> to be ' ' (0x20, i.e. space). Second, for ZEROPAD the check
> is hidden in the code.
>
> For better maintenance replace BUILD_BUG_ON() with static_assert()
> for ZEROPAD and move it closer to the definition. While at it,
> introduce check for SMALL.
>
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@xxxxxxxxxxxxxxx>
> ---
> lib/vsprintf.c | 5 ++++-
> 1 file changed, 4 insertions(+), 1 deletion(-)
>
> diff --git a/lib/vsprintf.c b/lib/vsprintf.c
> index 31a674dd2674..8a8ac7ce0289 100644
> --- a/lib/vsprintf.c
> +++ b/lib/vsprintf.c
> @@ -381,6 +381,9 @@ int num_to_str(char *buf, int size, unsigned long long num, unsigned int width)
> #define SMALL 32 /* use lowercase in hex (must be 32 == 0x20) */
> #define SPECIAL 64 /* prefix hex with "0x", octal with "0" */
>
> +static_assert(ZEROPAD + ' ' == '0');

I would have it match the comment above:

static_assert(ZEROPAD == ('0' - ' '));

But other than that:

Reviewed-by: Steven Rostedt (VMware) <rostedt@xxxxxxxxxxx>

-- Steve

> +static_assert(SMALL == ' ');
> +
> enum format_type {
> FORMAT_TYPE_NONE, /* Just a string part */
> FORMAT_TYPE_WIDTH,
> @@ -507,7 +510,7 @@ char *number(char *buf, char *end, unsigned long long num,
> /* zero or space padding */
> if (!(spec.flags & LEFT)) {
> char c = ' ' + (spec.flags & ZEROPAD);
> - BUILD_BUG_ON(' ' + ZEROPAD != '0');
> +
> while (--field_width >= 0) {
> if (buf < end)
> *buf = c;