[libvirt] [PATCH] qemudDomainAttachSCSIDisk: handle empty controller list

Jim Meyering jim at meyering.net
Tue Mar 2 16:54:05 UTC 2010


Clang found something that might be a real bug.
I suspect that ...drive.controller will always be at least one,
but we should not have to dive into the code trying to figure
that out.  It's easier/better here just to handle the potential trouble:

clang saw that if it *was* zero, then the following "for" loop
would not be entered, and "cont" would not be initialized.
On the very next statement "cont" (uninitialized) would be dereferenced.

>From 21ec1a8ae0218a5e8d789410318a973518ffec6c Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Tue, 2 Mar 2010 17:45:10 +0100
Subject: [PATCH] qemudDomainAttachSCSIDisk: handle empty controller list

* src/qemu/qemu_driver.c (qemudDomainAttachSCSIDisk): Handle
the (theoretical) case of an empty controller list, so that
clang does not think the subsequent dereference of "cont"
would dereference an undefined variable (due to preceding
loop not iterating even once).
---
 src/qemu/qemu_driver.c |    6 ++++++
 1 files changed, 6 insertions(+), 0 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 7f7c459..efb1857 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -5671,18 +5671,24 @@ static int qemudDomainAttachSCSIDisk(struct qemud_driver *driver,
         if (qemuAssignDeviceDiskAlias(disk, qemuCmdFlags) < 0)
             goto error;
         if (!(devstr = qemuBuildDriveDevStr(disk)))
             goto error;
     }

     if (!(drivestr = qemuBuildDriveStr(disk, 0, qemuCmdFlags)))
         goto error;

+    if (disk->info.addr.drive.controller <= 0) {
+        qemuReportError(VIR_ERR_INTERNAL_ERROR,
+                        _("no drive controller for %s"), disk->dst);
+        goto error;
+    }
+
     for (i = 0 ; i <= disk->info.addr.drive.controller ; i++) {
         cont = qemuDomainFindOrCreateSCSIDiskController(driver, vm, i, qemuCmdFlags);
         if (!cont)
             goto error;
     }

     if (cont->info.type != VIR_DOMAIN_DEVICE_ADDRESS_TYPE_PCI) {
         qemuReportError(VIR_ERR_INTERNAL_ERROR,
                         _("SCSI controller %d was missing its PCI address"), cont->idx);
--
1.7.0.1.414.g89213d




More information about the libvir-list mailing list