[libvirt] [PATCH] Avoid integer wrap on remotePortMax in QEMU driver

Eric Blake eblake at redhat.com
Thu Jan 17 19:04:55 UTC 2013


On 01/17/2013 05:09 AM, Daniel P. Berrange wrote:
> From: "Daniel P. Berrange" <berrange at redhat.com>
> 
> The QEMU driver default max port is 65535, but it then increments
> this by 1 to 65536. This maps to 0 in an unsigned short :-( This
> was apparently done so that for() loops could use "< max" instead
> of "<= max". Remove this insanity and just make the loop do the
> right thing.
> ---
>  src/qemu/qemu_conf.c        | 4 ----
>  src/util/virportallocator.c | 4 ++--
>  2 files changed, 2 insertions(+), 6 deletions(-)
> 

> @@ -103,7 +103,7 @@ int virPortAllocatorAcquire(virPortAllocatorPtr pa,
>      *port = 0;
>      virObjectLock(pa);
>  
> -    for (i = pa->start ; i < pa->end && !*port; i++) {
> +    for (i = pa->start ; i <= pa->end && !*port; i++) {

This won't work.  When pa->end is 65535 (the maximum value of unsigned
short), then the loop will iterate to 0, because you set up the loop
index to be unsigned short.  For things to work, you need this
additional patch:

diff --git i/src/util/virportallocator.c w/src/util/virportallocator.c
index d80347a..35f2157 100644
--- i/src/util/virportallocator.c
+++ w/src/util/virportallocator.c
@@ -97,7 +97,7 @@ int virPortAllocatorAcquire(virPortAllocatorPtr pa,
                             unsigned short *port)
 {
     int ret = -1;
-    unsigned short i;
+    int i;
     int fd = -1;

     *port = 0;

-- 
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: 621 bytes
Desc: OpenPGP digital signature
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20130117/3ad2edc3/attachment-0001.sig>


More information about the libvir-list mailing list