Handle failure of virConnectNumOfDomains and virConnectNumOfDefinedDomains
Changed malloc to use size of variable, not type
Removed unneeded NULL check before free
Removed unneeded initialization of several variables
Changed assignment to use the ternary operator
---
examples/hellolibvirt/hellolibvirt.c | 28 ++++++++++++++++++----------
1 files changed, 18 insertions(+), 10 deletions(-)
diff --git a/examples/hellolibvirt/hellolibvirt.c b/examples/hellolibvirt/hellolibvirt.c
index 22d3309..cc1af0f 100644
--- a/examples/hellolibvirt/hellolibvirt.c
+++ b/examples/hellolibvirt/hellolibvirt.c
@@ -1,6 +1,8 @@
/* This file contains trivial example code to connect to the running
* hypervisor and gather a few bits of information. */
+#include <config.h>
+
#include <stdio.h>
#include <stdlib.h>
#include <libvirt/libvirt.h>
@@ -52,12 +54,23 @@ showDomains(virConnectPtr conn)
char **nameList = NULL;
numActiveDomains = virConnectNumOfDomains(conn);
+ if (-1 == numActiveDomains) {
+ ret = 1;
+ printf("Failed to get number of active domains\n");
+ goto out;
+ }
+
numInactiveDomains = virConnectNumOfDefinedDomains(conn);
+ if (-1 == numInactiveDomains) {
+ ret = 1;
+ printf("Failed to get number of inactive domains\n");
+ goto out;
+ }
printf("There are %d active and %d inactive domains\n",
numActiveDomains, numInactiveDomains);
- nameList = malloc(sizeof(char *) * (unsigned int)numInactiveDomains);
+ nameList = malloc(sizeof(*nameList) * numInactiveDomains);
if (NULL == nameList) {
ret = 1;
@@ -88,10 +101,7 @@ showDomains(virConnectPtr conn)
}
out:
- if (NULL != nameList) {
- free(nameList);
- }
-
+ free(nameList);
return ret;
}
@@ -100,14 +110,12 @@ int
main(int argc, char *argv[])
{
int ret = 0;
- virConnectPtr conn = NULL;
- char *uri = NULL;
+ virConnectPtr conn;
+ char *uri;
printf("Attempting to connect to hypervisor\n");
- if (argc > 0) {
- uri = argv[1];
- }
+ uri = (argc > 0 ? argv[1] : NULL);
/* virConnectOpenAuth is called here with all default parameters,
* except, possibly, the URI of the hypervisor. */
--
1.6.0.6