]> git.ipfire.org Git - thirdparty/libvirt.git/commit
python: improve conversion validation
authorEric Blake <eblake@redhat.com>
Fri, 30 Mar 2012 18:03:20 +0000 (12:03 -0600)
committerEric Blake <eblake@redhat.com>
Sat, 31 Mar 2012 15:16:00 +0000 (09:16 -0600)
commit4a86c2bb4b2d8183b76ba44659bb6d2eab1f1573
tree6185d92523e88080bf2772550c2add8ce61d3e0a
parent8bf0442e833f93b88c0615bece90151041aaa0d4
python: improve conversion validation

Laszlo Ersek pointed out that in trying to convert a long to an
unsigned int, we used:

long long_val = ...;
if ((unsigned int)long_val == long_val)

According to C99 integer promotion rules, the if statement is
equivalent to:

(unsigned long)(unsigned int)long_val == (unsigned long)long_val

since you get an unsigned comparison if at least one side is
unsigned, using the largest rank of the two sides; but on 32-bit
platforms, where unsigned long and unsigned int are the same size,
this comparison is always true and ends up converting negative
long_val into posigive unsigned int values, rather than rejecting
the negative value as we had originally intended (python longs
are unbounded size, and we don't want to do silent modulo
arithmetic when converting to C code).

Fix this by using direct comparisons, rather than casting.

* python/typewrappers.c (libvirt_intUnwrap, libvirt_uintUnwrap)
(libvirt_ulongUnwrap, libvirt_ulonglongUnwrap): Fix conversion
checks.
python/typewrappers.c