[libvirt] [PATCH] maint: avoid potential promotion issues with [ug]id_t

Eric Blake eblake at redhat.com
Tue Jan 8 15:39:16 UTC 2013


On 01/07/2013 08:12 PM, Guannan Ren wrote:
>> * src/locking/lock_driver_sanlock.c
>> (virLockManagerSanlockSetupLockspace, virLockManagerSanlockInit)
>> (virLockManagerSanlockCreateLease): Cast -1 to proper type before
>> comparing with uid_t or gid_t.

> 
>     According to GUN C library
>     "uid_t This is an integer data type used to represent user IDs.
>              In the GNU library, this is an alias for unsigned int."
>      So driver->user = -1 will lead to wrong value.

No, actually 'driver->user = -1' is always the right value, no matter
what type uid_t is, because of C promotion rules (C guarantees that -1
promotes to the all-bits-set version of any unsigned type of any width).
 Where you get into problems is 'driver->user = driver->group = -1', in
the theoretical platform where driver->group is narrower than
driver->user and both types are unsigned (then you could end up with
0xffff instead of the intended 0xffffffff for driver->user).  Likewise,
on a platform where gid_t is 16-bit unsigned, then (gid_t)-1 == -1
promotes to '0xffff == -1' which is false.  Thankfully, I don't know of
any such perverse platforms, but better safe than sorry.

> 
> http://pubs.opengroup.org/onlinepubs/000095399/functions/chown.html
>      use (uid_t) -1 too.
> 
>      ACK.

Pushed.

-- 
Eric Blake   eblake redhat com    +1-919-301-3266
Libvirt virtualization library http://libvirt.org

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 619 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20130108/a435eff5/attachment-0001.sig>


More information about the libvir-list mailing list