[libvirt] [PATCH v2 0/7] Resolve Coverity issues

v1: https://www.redhat.com/archives/libvir-list/2013-July/msg00691.html Changes over v1: * Add additional files to patch #2 to include other places were sysconf(_SC_OPEN_MAX) was used within libvirt sources. * Change for loop end conditions to use "<variable> != -1" (should have looked deeper at other changes - wasn't even thinking of that option) John Ferlan (7): hellolibvirt: Resolve Coverity issues testutils: Resolve Coverity issues virsh-domain-monitor: Resolve Coverity issues virsh-interface: Resolve Coverity issues virsh-nodedev: Resolve Coverity issues storage_backend: Resolve Coverity issue qemu_hostdev: Resolve Coverity issue examples/hellolibvirt/hellolibvirt.c | 7 +++++++ src/lxc/lxc_container.c | 5 +++++ src/qemu/qemu_hostdev.c | 3 ++- src/storage/storage_backend.c | 1 + src/util/vircommand.c | 5 +++++ tests/commandhelper.c | 6 +++++- tests/testutils.c | 3 +++ tools/virsh-domain-monitor.c | 2 +- tools/virsh-interface.c | 4 ++-- tools/virsh-nodedev.c | 2 +- 10 files changed, 32 insertions(+), 6 deletions(-) -- 1.8.1.4

Recent changes uncovered a NEGATIVE_RETURNS when processing 'numNames' in 'showDomains' in the for loop after a possible -1 return. --- examples/hellolibvirt/hellolibvirt.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c index 83045b1..0179fad 100644 --- a/examples/hellolibvirt/hellolibvirt.c +++ b/examples/hellolibvirt/hellolibvirt.c @@ -90,6 +90,13 @@ showDomains(virConnectPtr conn) numNames = virConnectListAllDomains(conn, &nameList, flags); + if (numNames == -1) { + ret = 1; + printf("Failed to get a list of all domains: %s\n", + virGetLastErrorMessage()); + goto out; + } + for (i = 0; i < numNames; i++) { int active = virDomainIsActive(nameList[i]); printf(" %8s (%s)\n", -- 1.8.1.4

Recent changes uncovered a NEGATIVE_RETURNS in the return from sysconf() when processing a for loop in virtTestCaptureProgramExecChild() in testutils.c Code review uncovered 3 other code paths with the same condition that weren't found by Covirity, so fixed those as well. --- src/lxc/lxc_container.c | 5 +++++ src/util/vircommand.c | 5 +++++ tests/commandhelper.c | 6 +++++- tests/testutils.c | 3 +++ 4 files changed, 18 insertions(+), 1 deletion(-) diff --git a/src/lxc/lxc_container.c b/src/lxc/lxc_container.c index 257cf93..0e3fa0b 100644 --- a/src/lxc/lxc_container.c +++ b/src/lxc/lxc_container.c @@ -247,6 +247,11 @@ static int lxcContainerSetStdio(int control, int ttyfd, int handshakefd) /* Just in case someone forget to set FD_CLOEXEC, explicitly * close all FDs before executing the container */ open_max = sysconf(_SC_OPEN_MAX); + if (open_max < 0) { + virReportSystemError(errno, "%s", + _("sysconf(_SC_OPEN_MAX) failed")); + goto cleanup; + } for (fd = 0; fd < open_max; fd++) if (fd != ttyfd && fd != control && fd != handshakefd) { int tmpfd = fd; diff --git a/src/util/vircommand.c b/src/util/vircommand.c index 3529f1a..033b55b 100644 --- a/src/util/vircommand.c +++ b/src/util/vircommand.c @@ -511,6 +511,11 @@ virExec(virCommandPtr cmd) } openmax = sysconf(_SC_OPEN_MAX); + if (openmax < 0) { + virReportSystemError(errno, "%s", + _("sysconf(_SC_OPEN_MAX) failed")); + goto fork_error; + } for (fd = 3; fd < openmax; fd++) { if (fd == childin || fd == childout || fd == childerr) continue; diff --git a/tests/commandhelper.c b/tests/commandhelper.c index 0c5aa82..296fbbb 100644 --- a/tests/commandhelper.c +++ b/tests/commandhelper.c @@ -58,6 +58,7 @@ static int envsort(const void *a, const void *b) { int main(int argc, char **argv) { size_t i, n; + int open_max; char **origenv; char **newenv; char *cwd; @@ -96,7 +97,10 @@ int main(int argc, char **argv) { fprintf(log, "ENV:%s\n", newenv[i]); } - for (i = 0; i < sysconf(_SC_OPEN_MAX); i++) { + open_max = sysconf(_SC_OPEN_MAX); + if (open_max < 0) + return EXIT_FAILURE; + for (i = 0; i < open_max; i++) { int f; int closed; if (i == fileno(log)) diff --git a/tests/testutils.c b/tests/testutils.c index ec0fe52..2fdf7b8 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -281,6 +281,9 @@ void virtTestCaptureProgramExecChild(const char *const argv[], goto cleanup; open_max = sysconf(_SC_OPEN_MAX); + if (open_max < 0) + goto cleanup; + for (i = 0; i < open_max; i++) { if (i != stdinfd && i != pipefd) { -- 1.8.1.4

Recent changes uncovered a pair of NEGATIVE_RETURNS when processing the 'nnames' in 'vshDomainListCollect' in the for loop due to possible -1 value. --- tools/virsh-domain-monitor.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/virsh-domain-monitor.c b/tools/virsh-domain-monitor.c index 7af765e..5fbd32c 100644 --- a/tools/virsh-domain-monitor.c +++ b/tools/virsh-domain-monitor.c @@ -1599,7 +1599,7 @@ finished: success = true; cleanup: - for (i = 0; i < nnames; i++) + for (i = 0; nnames != -1 && i < nnames; i++) VIR_FREE(names[i]); if (!success) { -- 1.8.1.4

Recent changes uncovered FORWARD_NULL and NEGATIVE_RETURNS problems with the processing of the 'nActiveIfaces' and 'nInactiveIfaces' and their associated allocated arrays in 'vshInterfaceListCollect' due to the possibility of returning -1 in a call and using the return value as a for loop index end condition. --- tools/virsh-interface.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tools/virsh-interface.c b/tools/virsh-interface.c index 9fdd36e..0f78551 100644 --- a/tools/virsh-interface.c +++ b/tools/virsh-interface.c @@ -300,10 +300,10 @@ finished: success = true; cleanup: - for (i = 0; i < nActiveIfaces; i++) + for (i = 0; nActiveIfaces != -1 && i < nActiveIfaces; i++) VIR_FREE(activeNames[i]); - for (i = 0; i < nInactiveIfaces; i++) + for (i = 0; nInactiveIfaces != -1 && i < nInactiveIfaces; i++) VIR_FREE(inactiveNames[i]); VIR_FREE(activeNames); -- 1.8.1.4

Recent changes uncovered FORWARD_NULL and NEGATIVE_RETURNS problems with the processing of the 'ndevices' and its associated allocated arrays in 'vshNodeDeviceListCollect' due to the possibility of returning -1 in a call and using the returned value as a for loop index end condition. --- tools/virsh-nodedev.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tools/virsh-nodedev.c b/tools/virsh-nodedev.c index 2eb1979..3255079 100644 --- a/tools/virsh-nodedev.c +++ b/tools/virsh-nodedev.c @@ -352,7 +352,7 @@ finished: success = true; cleanup: - for (i = 0; i < ndevices; i++) + for (i = 0; ndevices != -1 && i < ndevices; i++) VIR_FREE(names[i]); VIR_FREE(names); -- 1.8.1.4

The switch statement in 'virStorageBackendCreateQemuImgOpts' used the for loop end condition 'VIR_STORAGE_FILE_FEATURE_LAST' as a possible value, but since that cannot happen Coverity spits out a DEADCODE message. Adding the Coverity tag just removes the Coverity message --- src/storage/storage_backend.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/storage/storage_backend.c b/src/storage/storage_backend.c index 5d06eb6..8d5880e 100644 --- a/src/storage/storage_backend.c +++ b/src/storage/storage_backend.c @@ -663,6 +663,7 @@ virStorageBackendCreateQemuImgOpts(char **opts, } break; + /* coverity[dead_error_begin] */ case VIR_STORAGE_FILE_FEATURE_LAST: ; } -- 1.8.1.4

Recent changes uncovered a possibility that 'last_processed_hostdev_vf' was set to -1 in 'qemuPrepareHostdevPCIDevices' and would cause problems in for loop end condition in the 'resetvfnetconfig' label if the variable was never set to 'i' due to 'qemuDomainHostdevNetConfigReplace' failure. --- src/qemu/qemu_hostdev.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/qemu/qemu_hostdev.c b/src/qemu/qemu_hostdev.c index 9bdace1..f24d466 100644 --- a/src/qemu/qemu_hostdev.c +++ b/src/qemu/qemu_hostdev.c @@ -644,7 +644,8 @@ inactivedevs: } resetvfnetconfig: - for (i = 0; i < last_processed_hostdev_vf; i++) { + for (i = 0; last_processed_hostdev_vf != -1 && + i < last_processed_hostdev_vf; i++) { virDomainHostdevDefPtr hostdev = hostdevs[i]; if (hostdev->parent.type == VIR_DOMAIN_DEVICE_NET && hostdev->parent.data.net) { -- 1.8.1.4

On Thu, Jul 11, 2013 at 11:16:22AM -0400, John Ferlan wrote:
v1: https://www.redhat.com/archives/libvir-list/2013-July/msg00691.html
Changes over v1: * Add additional files to patch #2 to include other places were sysconf(_SC_OPEN_MAX) was used within libvirt sources. * Change for loop end conditions to use "<variable> != -1" (should have looked deeper at other changes - wasn't even thinking of that option)
John Ferlan (7): hellolibvirt: Resolve Coverity issues testutils: Resolve Coverity issues virsh-domain-monitor: Resolve Coverity issues virsh-interface: Resolve Coverity issues virsh-nodedev: Resolve Coverity issues storage_backend: Resolve Coverity issue qemu_hostdev: Resolve Coverity issue
ACK to all Daniel -- |: http://berrange.com -o- http://www.flickr.com/photos/dberrange/ :| |: http://libvirt.org -o- http://virt-manager.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: http://entangle-photo.org -o- http://live.gnome.org/gtk-vnc :|

On 07/11/2013 12:02 PM, Daniel P. Berrange wrote:
On Thu, Jul 11, 2013 at 11:16:22AM -0400, John Ferlan wrote:
v1: https://www.redhat.com/archives/libvir-list/2013-July/msg00691.html
Changes over v1: * Add additional files to patch #2 to include other places were sysconf(_SC_OPEN_MAX) was used within libvirt sources. * Change for loop end conditions to use "<variable> != -1" (should have looked deeper at other changes - wasn't even thinking of that option)
John Ferlan (7): hellolibvirt: Resolve Coverity issues testutils: Resolve Coverity issues virsh-domain-monitor: Resolve Coverity issues virsh-interface: Resolve Coverity issues virsh-nodedev: Resolve Coverity issues storage_backend: Resolve Coverity issue qemu_hostdev: Resolve Coverity issue
ACK to all
Daniel
Pushed Thanks for the quick reviews! John
participants (2)
-
Daniel P. Berrange
-
John Ferlan