Re: [PATCH] thermal: k3_j72xx_bandgap: Fix array underflow in prep_lookup_table()

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sat, May 21, 2022 at 09:56:16AM +0300, Dan Carpenter wrote:
> On Fri, May 20, 2022 at 05:25:56PM +0200, Daniel Lezcano wrote:
> > On 20/05/2022 17:02, Dan Carpenter wrote:
> > > This while loop exits with "i" set to -1 and so then it sets:
> > 
> > Won't it exit with 'i' set to '0' ?
> > 
> 
> Wow.  You made me worried there.  I had to make a test case just to be
> sure:
> 
>         int i = 10;
> 
>         while (i--)
>                 printf("in %d\n", i);
>         printf("out %d\n", i);
> 
> Yep.  Ends on on -1.

I wrote a blog about this a few days back.

https://staticthinking.wordpress.com/2022/05/17/i-or-i/

I really think the most readable way is to say:

	while (--i >= 0)
		derived_table[i] = derived_table[i + 1] - 300;

Some people like while (i--) because it works on unsigned variables but
that doesn't apply here and "unsigned int i;" is dumb.

regards,
dan carpenter




[Index of Archives]     [Kernel Development]     [Kernel Announce]     [Kernel Newbies]     [Linux Networking Development]     [Share Photos]     [IDE]     [Security]     [Git]     [Netfilter]     [Yosemite News]     [MIPS Linux]     [ARM Linux]     [Device Mapper]

  Powered by Linux