Re: [PATCH 8/8] drm/client: s/unsigned int i/int i/

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

 



On Mon, Oct 07, 2024 at 09:43:47AM +0200, Thomas Zimmermann wrote:
> Hi
> 
> Am 03.10.24 um 13:33 schrieb Ville Syrjala:
> > From: Ville Syrjälä <ville.syrjala@xxxxxxxxxxxxxxx>
> >
> > Replace the 'unsigned int i' footguns with plain old signed
> > int. Avoids accidents if/when someone decides they need
> > to iterate backwards.
> 
> Why are signed types preferable here?

If you iterate backwards you typically write

for (i = max; i >= 0; i--) {...}

and i>=0 is always true for unsigned types.

Another danger is doing any kind of arithmetic
with 'i' and expecting a signed result.

Based on my experience in getting burned by C integer
promotion/converison rules a good rule of thumb is to
always use just "int" unless there is a very good
reason for not doing so (eg. if the thing is a bitmask
or some kind of other thing where negative values
can never ever come up).

Also IIRC there was a Linus rant about "unsigned int i"
but I can't find it now.

-- 
Ville Syrjälä
Intel



[Index of Archives]     [AMD Graphics]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux