mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 11:35:19 +00:00
maint: improve VIR_ERR_INVALID_DOMAIN usage
In datatype.c, virGetDomainSnapshot could result in the message: error: invalid domain pointer in bad domain Furthermore, while there are a few functions in libvirt.c that only care about a virDomainPtr without regards to the connection (such as virDomainGetName), most functions also require a valid connection. Yet several functions were blindly dereferencing the conn member without checking it for validity first (such as virDomainOpenConsole). Rather than try and correct all usage of VIR_IS_DOMAIN vs. VIR_IS_CONNECTED_DOMAIN, it is easier to just blindly require that a valid domain object always has a valid connection object (which should be true anyways, since every domain object holds a reference to its connection, so the connection will not be closed until all domain objects have also been closed to release their reference). After this patch, all places that validate a domain consistently report: error: invalid domain pointer in someFunc * src/datatypes.h (virCheckDomainReturn, virCheckDomainGoto): New macros. * src/datatypes.c (virGetDomainSnapshot): Use new macro. (virLibConnError): Delete unused macro. Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
parent
36dd0bd88a
commit
6e130ddc4d
@ -32,7 +32,6 @@ src/cpu/cpu_generic.c
|
||||
src/cpu/cpu_map.c
|
||||
src/cpu/cpu_powerpc.c
|
||||
src/cpu/cpu_x86.c
|
||||
src/datatypes.c
|
||||
src/driver.c
|
||||
src/esx/esx_driver.c
|
||||
src/esx/esx_network_driver.c
|
||||
|
@ -31,11 +31,6 @@
|
||||
|
||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||
|
||||
#define virLibConnError(code, ...) \
|
||||
virReportErrorHelper(VIR_FROM_THIS, code, __FILE__, \
|
||||
__FUNCTION__, __LINE__, __VA_ARGS__)
|
||||
|
||||
|
||||
virClassPtr virConnectClass;
|
||||
virClassPtr virConnectCloseCallbackDataClass;
|
||||
virClassPtr virDomainClass;
|
||||
@ -796,22 +791,19 @@ virGetDomainSnapshot(virDomainPtr domain, const char *name)
|
||||
if (virDataTypesInitialize() < 0)
|
||||
return NULL;
|
||||
|
||||
if (!VIR_IS_DOMAIN(domain)) {
|
||||
virLibConnError(VIR_ERR_INVALID_DOMAIN, "%s", _("bad domain"));
|
||||
return NULL;
|
||||
}
|
||||
virCheckNonNullArgReturn(name, NULL);
|
||||
virCheckDomainGoto(domain, error);
|
||||
virCheckNonNullArgGoto(name, error);
|
||||
|
||||
if (!(ret = virObjectNew(virDomainSnapshotClass)))
|
||||
return NULL;
|
||||
goto error;
|
||||
if (VIR_STRDUP(ret->name, name) < 0)
|
||||
goto cleanup;
|
||||
goto error;
|
||||
|
||||
ret->domain = virObjectRef(domain);
|
||||
|
||||
return ret;
|
||||
|
||||
cleanup:
|
||||
error:
|
||||
virObjectUnref(ret);
|
||||
return NULL;
|
||||
}
|
||||
|
@ -64,6 +64,29 @@ extern virClassPtr virStoragePoolClass;
|
||||
(virObjectIsClass((obj), virDomainClass))
|
||||
# define VIR_IS_CONNECTED_DOMAIN(obj) \
|
||||
(VIR_IS_DOMAIN(obj) && virObjectIsClass((obj)->conn, virConnectClass))
|
||||
# define virCheckDomainReturn(obj, retval) \
|
||||
do { \
|
||||
virDomainPtr _dom = (obj); \
|
||||
if (!virObjectIsClass(_dom, virDomainClass) || \
|
||||
!virObjectIsClass(_dom->conn, virConnectClass)) { \
|
||||
virReportErrorHelper(VIR_FROM_DOM, VIR_ERR_INVALID_DOMAIN, \
|
||||
__FILE__, __FUNCTION__, __LINE__, \
|
||||
__FUNCTION__); \
|
||||
virDispatchError(NULL); \
|
||||
return retval; \
|
||||
} \
|
||||
} while (0)
|
||||
# define virCheckDomainGoto(obj, label) \
|
||||
do { \
|
||||
virDomainPtr _dom = (obj); \
|
||||
if (!virObjectIsClass(_dom, virDomainClass) || \
|
||||
!virObjectIsClass(_dom->conn, virConnectClass)) { \
|
||||
virReportErrorHelper(VIR_FROM_DOM, VIR_ERR_INVALID_DOMAIN, \
|
||||
__FILE__, __FUNCTION__, __LINE__, \
|
||||
__FUNCTION__); \
|
||||
goto label; \
|
||||
} \
|
||||
} while (0)
|
||||
|
||||
# define VIR_IS_NETWORK(obj) \
|
||||
(virObjectIsClass((obj), virNetworkClass))
|
||||
|
@ -36,10 +36,6 @@
|
||||
|
||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||
|
||||
#define virLibDomainError(domain, error, info) \
|
||||
virReportErrorHelper(VIR_FROM_DOM, error, __FILE__, __FUNCTION__, \
|
||||
__LINE__, info)
|
||||
|
||||
/**
|
||||
* virDomainLxcOpenNamespace:
|
||||
* @domain: a domain object
|
||||
@ -70,12 +66,7 @@ virDomainLxcOpenNamespace(virDomainPtr domain,
|
||||
|
||||
virResetLastError();
|
||||
|
||||
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
||||
virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
||||
virDispatchError(NULL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
virCheckDomainReturn(domain, -1);
|
||||
conn = domain->conn;
|
||||
|
||||
virCheckNonNullArgGoto(fdlist, error);
|
||||
|
@ -30,10 +30,6 @@
|
||||
|
||||
#define VIR_FROM_THIS VIR_FROM_NONE
|
||||
|
||||
#define virLibDomainError(domain, error, info) \
|
||||
virReportErrorHelper(VIR_FROM_DOM, error, __FILE__, __FUNCTION__, \
|
||||
__LINE__, info)
|
||||
|
||||
/**
|
||||
* virDomainQemuMonitorCommand:
|
||||
* @domain: a domain object
|
||||
@ -77,12 +73,7 @@ virDomainQemuMonitorCommand(virDomainPtr domain, const char *cmd,
|
||||
|
||||
virResetLastError();
|
||||
|
||||
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
||||
virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
||||
virDispatchError(NULL);
|
||||
return -1;
|
||||
}
|
||||
|
||||
virCheckDomainReturn(domain, -1);
|
||||
conn = domain->conn;
|
||||
|
||||
virCheckNonNullArgGoto(result, error);
|
||||
@ -205,12 +196,7 @@ virDomainQemuAgentCommand(virDomainPtr domain,
|
||||
|
||||
virResetLastError();
|
||||
|
||||
if (!VIR_IS_CONNECTED_DOMAIN(domain)) {
|
||||
virLibDomainError(NULL, VIR_ERR_INVALID_DOMAIN, __FUNCTION__);
|
||||
virDispatchError(NULL);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
virCheckDomainReturn(domain, NULL);
|
||||
conn = domain->conn;
|
||||
|
||||
virCheckReadOnlyGoto(conn->flags, error);
|
||||
|
1052
src/libvirt.c
1052
src/libvirt.c
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue
Block a user