Re: [PATCH 09/11] staging: comedi: comedi_buf: clarify comedi_buf_read_alloc()

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

 



On 02/01/2013 19:12, H Hartley Sweeten wrote:
On Wednesday, January 02, 2013 12:03 PM, Ian Abbott wrote:
On 02/01/2013 18:10, H Hartley Sweeten wrote:
Will the munge_count always be larger, or equal, to the buf_read_alloc_count?
The original code had the (int) cast so I wasn't sure. If so, then yes the variable
should be an unsigned int.

Well the value it is being compared to (nbytes) is an unsigned int, so
the int value (available) will be converted to unsigned int before the
comparison anyway.  So making 'available' an unsigned int to begin with
shouldn't break anything that isn't already broke!

Not so. Simple test:


#include <stdio.h>
#include <stdlib.h>

int main(int argv, char *argc[])
{
         unsigned int var1;
         int var2;

         var1 = 1000;
         if (99 > var1)
                 printf("1. 99 > %d\n", var1);

         var1 = -40;
         if (99 > var1)
                 printf("2. 99 > %d\n", var1);

         var2 = 1000;
         if (99 > var2)
                 printf("3. 99 > %d\n", var2);

         var2 = -40;
         if (99 > var2)
                 printf("4. 99 > %d\n", var2);

         return 0;
}

$ ./a.out
4. 99 > -40

If you want to compare a possibly negative value to the unsigned nbytes then you'd better cast nbytes to a signed int first. However, munge_count should always be greater than or equal to buf_read_alloc_count so available should not be negative. Or rather, not until you've munged more than 1<<32 bytes when it all goes horribly wrong!

--
-=( Ian Abbott @ MEV Ltd.    E-mail: <abbotti@xxxxxxxxx>        )=-
-=( Tel: +44 (0)161 477 1898   FAX: +44 (0)161 718 3587         )=-


_______________________________________________
devel mailing list
devel@xxxxxxxxxxxxxxxxxxxxxx
http://driverdev.linuxdriverproject.org/mailman/listinfo/devel


[Index of Archives]     [Linux Driver Backports]     [DMA Engine]     [Linux GPIO]     [Linux SPI]     [Video for Linux]     [Linux USB Devel]     [Linux Coverity]     [Linux Audio Users]     [Linux Kernel]     [Linux SCSI]     [Yosemite Backpacking]
  Powered by Linux