Problems with libdivecomputer under OSX

Jef Driesen jefdriesen at telenet.be
Sat Jul 7 10:29:26 UTC 2012


On 07/07/2012 11:09 AM, Henrik wrote:
> On 07.07.12 10:59, Jef Driesen wrote:
>> I'm not seeing any problems on 64bit linux, but maybe the actual value fits into
>> an int, and on macosx it does not? Can someone check the actual values for
>> TIOCMBIC and TIOCMBIS on 32 and 64bit macosx (using unsigned long of course).
>
> Is this the way to do it?
>
>       printf("%ld %ld\n", TIOCMBIS, TIOCMBIC);
>
> In that case, I've got this for 32bit:
>
>       -2147191700 -2147191701
>
> ... and this for 64bit:
>
>       2147775596 2147775595

I already see what's the problem. These are 32bit negative values, and thus have 
the sign bit (the highest bit) set. For example TIOCMBIS is 0x8004746c. When 
signed integers are casted to a type with more bits (e.g. 32bit to 64bit) sign 
extensions is done to preserve the negative value. For TIOCMBIS the result would 
be 0xffffffff8004746c. But when interpreted as an unsigned long (which is 
probably what is done inside the ioctl call), it's a very different number of 
course.

For 32bit no sign extension is done, so no problem. On linux, the numbers are 
much smaller (e.g. TIOCMBIS is 21526), and thus the sign bit isn't set, so no 
problem on 32 and 64bit.

Thus the solution will be to use an unsigned type for the action variable, 
rather than a signed type. Both unsigned int and unsigned long should work fine.

Jef




More information about the Devel mailing list