]> xenbits.xensource.com Git - libvirt.git/commitdiff
Avoid integer wrap on remotePortMax in QEMU driver
authorDaniel P. Berrange <berrange@redhat.com>
Thu, 17 Jan 2013 12:07:53 +0000 (12:07 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 17 Jan 2013 13:52:33 +0000 (13:52 +0000)
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
src/util/virportallocator.c
tests/virportallocatortest.c

index 680e8fb878f9ddba5cacbb5b2a555d4faf9bc528..b21392e2ce72072de6b2a17bf71511f25e7c29cf 100644 (file)
@@ -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,
index 033aee47026fca86b3e5629be40aed14e2e22633..d80347ad8b03ef3ce1687fbde5e892e494574f24 100644 (file)
@@ -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;
@@ -168,7 +168,7 @@ int virPortAllocatorRelease(virPortAllocatorPtr pa,
     virObjectLock(pa);
 
     if (port < pa->start ||
-        port >= pa->end) {
+        port > pa->end) {
         virReportInvalidArg(port, "port %d must be in range (%d, %d)",
                             port, pa->start, pa->end);
         goto cleanup;
index 93577d7ff7a67160ac3f6ae5330ec12478c61029..3f6edcc3947c94b6daced737626489b862a88e89 100644 (file)
@@ -59,7 +59,7 @@ int bind(int sockfd ATTRIBUTE_UNUSED,
 
 static int testAllocAll(const void *args ATTRIBUTE_UNUSED)
 {
-    virPortAllocatorPtr alloc = virPortAllocatorNew(5900, 5910);
+    virPortAllocatorPtr alloc = virPortAllocatorNew(5900, 5909);
     int ret = -1;
     unsigned short p1, p2, p3, p4, p5, p6, p7;