maint: improve VIR_ERR_INVALID_CONN usage

The datatype.c object checks could result in a message like:

error: invalid connection pointer in no connection

This consolidates all clients of this message to have uniform contents:

error: invalid connection pointer in someFunc

Note that virCheckConnectReturn raises an error immediately; in
datatypes.c, where we don't need to raise the error (but instead
just leave it in the thread-local setting), we use
virCheckConnectGoto and the cleanup label instead.  Then, for
consistency in that file, all subsequent error messages are
touched to also use the cleanup error label.

* src/datatypes.h (virCheckConnectReturn)
(virCheckConnectGoto): New macros.
* src/datatypes.c: Use new macro.
* src/libvirt-qemu.c (virDomainQemuAttach): Likewise.
(virLibConnError): Delete unused macro.
* src/libvirt-lxc.c (virLibConnError): Likewise.
* src/libvirt.c: Use new macro throughout.
* docs/api_extension.html.in: Modernize documentation.

Signed-off-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Eric Blake 2013-12-27 20:31:17 -07:00
parent 7c98d1c153
commit db3dd0824f
6 changed files with 178 additions and 732 deletions

View File

@ -180,12 +180,13 @@
being called and its parameters;</li>
<li>MUST call virResetLastError();</li>
<li>SHOULD confirm that the connection is valid with
VIR_IS_CONNECT(conn);</li>
virCheckConnectReturn() or virCheckConnectGoto();</li>
<li><strong>SECURITY: If the API requires a connection with write
privileges, MUST confirm that the connection flags do not
indicate that the connection is read-only;</strong></li>
indicate that the connection is read-only with
virCheckReadOnlyGoto();</strong></li>
<li>SHOULD do basic validation of the parameters that are being
passed in;</li>
passed in, using helpers like virCheckNonNullArgGoto();</li>
<li>MUST confirm that the driver for this connection exists and that
it implements this function;</li>
<li>MUST call the internal API;</li>

View File

@ -1,7 +1,7 @@
/*
* datatypes.h: management of structs for public data types
*
* Copyright (C) 2006-2012 Red Hat, Inc.
* Copyright (C) 2006-2014 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
@ -218,15 +218,12 @@ virGetDomain(virConnectPtr conn, const char *name, const unsigned char *uuid)
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckNonNullArgReturn(uuid, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
virCheckNonNullArgGoto(uuid, error);
if (!(ret = virObjectNew(virDomainClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0)
goto error;
@ -289,15 +286,12 @@ virGetNetwork(virConnectPtr conn, const char *name, const unsigned char *uuid)
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckNonNullArgReturn(uuid, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
virCheckNonNullArgGoto(uuid, error);
if (!(ret = virObjectNew(virNetworkClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0)
goto error;
@ -360,18 +354,15 @@ virGetInterface(virConnectPtr conn, const char *name, const char *mac)
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
/* a NULL mac from caller is okay. Treat it as blank */
if (mac == NULL)
mac = "";
if (!(ret = virObjectNew(virInterfaceClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0 ||
VIR_STRDUP(ret->mac, mac) < 0)
@ -435,15 +426,12 @@ virGetStoragePool(virConnectPtr conn, const char *name,
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckNonNullArgReturn(uuid, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
virCheckNonNullArgGoto(uuid, error);
if (!(ret = virObjectNew(virStoragePoolClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0)
goto error;
@ -518,16 +506,13 @@ virGetStorageVol(virConnectPtr conn, const char *pool, const char *name,
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(pool, NULL);
virCheckNonNullArgReturn(name, NULL);
virCheckNonNullArgReturn(key, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(pool, error);
virCheckNonNullArgGoto(name, error);
virCheckNonNullArgGoto(key, error);
if (!(ret = virObjectNew(virStorageVolClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->pool, pool) < 0 ||
VIR_STRDUP(ret->name, name) < 0 ||
@ -597,14 +582,11 @@ virGetNodeDevice(virConnectPtr conn, const char *name)
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
if (!(ret = virObjectNew(virNodeDeviceClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0)
goto error;
@ -664,12 +646,9 @@ virGetSecret(virConnectPtr conn, const unsigned char *uuid,
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(uuid, NULL);
virCheckNonNullArgReturn(usageID, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(uuid, error);
virCheckNonNullArgGoto(usageID, error);
if (!(ret = virObjectNew(virSecretClass)))
return NULL;
@ -761,15 +740,12 @@ virGetNWFilter(virConnectPtr conn, const char *name,
if (virDataTypesInitialize() < 0)
return NULL;
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(VIR_ERR_INVALID_CONN, "%s", _("no connection"));
return NULL;
}
virCheckNonNullArgReturn(name, NULL);
virCheckNonNullArgReturn(uuid, NULL);
virCheckConnectGoto(conn, error);
virCheckNonNullArgGoto(name, error);
virCheckNonNullArgGoto(uuid, error);
if (!(ret = virObjectNew(virNWFilterClass)))
return NULL;
goto error;
if (VIR_STRDUP(ret->name, name) < 0)
goto error;

View File

@ -42,6 +42,25 @@ extern virClassPtr virStoragePoolClass;
# define VIR_IS_CONNECT(obj) \
(virObjectIsClass((obj), virConnectClass))
# define virCheckConnectReturn(obj, retval) \
do { \
if (!VIR_IS_CONNECT(obj)) { \
virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \
__FILE__, __FUNCTION__, __LINE__, \
__FUNCTION__); \
virDispatchError(NULL); \
return retval; \
} \
} while (0)
# define virCheckConnectGoto(obj, label) \
do { \
if (!VIR_IS_CONNECT(obj)) { \
virReportErrorHelper(VIR_FROM_THIS, VIR_ERR_INVALID_CONN, \
__FILE__, __FUNCTION__, __LINE__, \
__FUNCTION__); \
goto label; \
} \
} while (0)
# define VIR_IS_DOMAIN(obj) \
(virObjectIsClass((obj), virDomainClass))

View File

@ -36,10 +36,6 @@
#define VIR_FROM_THIS VIR_FROM_NONE
#define virLibConnError(conn, error, info) \
virReportErrorHelper(VIR_FROM_NONE, error, __FILE__, __FUNCTION__, \
__LINE__, info)
#define virLibDomainError(domain, error, info) \
virReportErrorHelper(VIR_FROM_DOM, error, __FILE__, __FUNCTION__, \
__LINE__, info)

View File

@ -30,10 +30,6 @@
#define VIR_FROM_THIS VIR_FROM_NONE
#define virLibConnError(conn, error, info) \
virReportErrorHelper(VIR_FROM_NONE, error, __FILE__, __FUNCTION__, \
__LINE__, info)
#define virLibDomainError(domain, error, info) \
virReportErrorHelper(VIR_FROM_DOM, error, __FILE__, __FUNCTION__, \
__LINE__, info)
@ -149,12 +145,7 @@ virDomainQemuAttach(virConnectPtr conn,
virResetLastError();
if (!VIR_IS_CONNECT(conn)) {
virLibConnError(NULL, VIR_ERR_INVALID_CONN, __FUNCTION__);
virDispatchError(NULL);
return NULL;
}
virCheckConnectReturn(conn, NULL);
virCheckPositiveArgGoto(pid_value, error);
if (pid != pid_value) {
virReportInvalidArg(pid_value,

File diff suppressed because it is too large Load Diff