Re: [PATCH] linux/bits.h: fix -Wtype-limits warnings in GENMASK_INPUT_CHECK()

From: Andy Shevchenko
Date: Mon Mar 07 2022 - 11:32:16 EST


On Mon, Mar 7, 2022 at 5:10 PM Alexander Lobakin
<alexandr.lobakin@xxxxxxxxx> wrote:
> From: Vincent MAILHOL <mailhol.vincent@xxxxxxxxxx>
> Date: Mon, 7 Mar 2022 22:50:56 +0900

...

> For example, people tend to make the following mistake:
>
> unsigned int i;
>
> for (i = 0; i ...) {
> ret = setup_something(array[i]);
> if (ret)
> goto unroll;
> }
>
> unroll:
> while (--i)
> unroll_something(array[i]);
>
> The loop will never end as `i` was declared as unsigned.
> -Wtype-limits catches this.

This looks like a wrapping value issue, not sure if the type limits
makes logical sense. What I'm saying is that the waning is
controversial. It may help or it may make noise.

> Not speaking of checking unsigned variables on < 0:
>
> unsigned int num;
>
> /* calculate_something() returns the number of something
> * or -ERRNO in case of an error
> */
> num = calculate_something();
> if (num < 0)
> ...

Depends on the context. Here is a mistake, but there are plenty of
cases when it's okay to do so. And in the above the variable name is
misleading with its semantics, The proper code should be

unsigned int num;
int ret;

ret = ...
if (ret < 0)
...
num = ret;

Again, the warning is controversial in my opinion.

--
With Best Regards,
Andy Shevchenko