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

From: "Daniel P. Berrange" <berrange@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(-) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 680e8fb..b21392e 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -233,10 +233,6 @@ int qemuLoadDriverConfig(virQEMUDriverPtr driver, filename, QEMU_REMOTE_PORT_MAX); goto cleanup; } - /* increasing the value by 1 makes all the loops going through - the bitmap (i = remotePortMin; i < remotePortMax; i++), work as - expected. */ - driver->remotePortMax++; if (driver->remotePortMin > driver->remotePortMax) { virReportError(VIR_ERR_INTERNAL_ERROR, diff --git a/src/util/virportallocator.c b/src/util/virportallocator.c index 033aee4..5d07dd0 100644 --- a/src/util/virportallocator.c +++ b/src/util/virportallocator.c @@ -84,7 +84,7 @@ virPortAllocatorPtr virPortAllocatorNew(unsigned short start, pa->start = start; pa->end = end; - if (!(pa->bitmap = virBitmapNew(end-start))) { + if (!(pa->bitmap = virBitmapNew((end-start)+1))) { virReportOOMError(); virObjectUnref(pa); return NULL; @@ -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++) { int reuse = 1; struct sockaddr_in addr; bool used = false; -- 1.8.0.2

On 01/17/13 13:09, Daniel P. Berrange wrote:
@@ -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++) { int reuse = 1; struct sockaddr_in addr; bool used = false;
The same condition needs to be changed in virPortAllocatorRelease. (And this breaks the virportallocatortest)

On 01/17/2013 05:09 AM, Daniel P. Berrange wrote:
From: "Daniel P. Berrange" <berrange@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
participants (3)
-
Daniel P. Berrange
-
Eric Blake
-
Ján Tomko