lkml.org 
[lkml]   [2022]   [Mar]   [7]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
From
Date
SubjectRe: [PATCH] linux/bits.h: fix -Wtype-limits warnings in GENMASK_INPUT_CHECK()
On Mon, Mar 7, 2022 at 5:10 PM Alexander Lobakin
<alexandr.lobakin@intel.com> wrote:
> From: Vincent MAILHOL <mailhol.vincent@wanadoo.fr>
> 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

\
 
 \ /
  Last update: 2022-03-07 17:33    [W:0.055 / U:0.384 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site