[libvirt] [PATCH 1/5] internal: Introduce virCheckNonEmptyStringArgGoto and reuse it

Peter Krempa pkrempa at redhat.com
Tue Jun 23 17:15:37 UTC 2015


The helper makes sure that strings passed to APIs are non-NULL and
non-empty. This allows to drop some inlined checks where it does not
make sense.
---
 src/internal.h         | 11 +++++++++++
 src/libvirt-domain.c   |  4 ++--
 src/qemu/qemu_driver.c | 11 -----------
 src/util/virerror.h    | 11 +++++++++++
 4 files changed, 24 insertions(+), 13 deletions(-)

diff --git a/src/internal.h b/src/internal.h
index 7c042e0..db26fb0 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -446,6 +446,17 @@
             goto label;                             \
         }                                           \
     } while (0)
+# define virCheckNonEmptyStringArgGoto(argname, label) \
+    do {                                               \
+        if (argname == NULL) {                         \
+            virReportInvalidNonNullArg(argname);       \
+            goto label;                                \
+        }                                              \
+        if (*argname == '\0') {                        \
+            virReportInvalidEmptyStringArg(argname);   \
+            goto label;                                \
+        }                                              \
+    } while (0)
 # define virCheckPositiveArgGoto(argname, label)    \
     do {                                            \
         if (argname <= 0) {                         \
diff --git a/src/libvirt-domain.c b/src/libvirt-domain.c
index 4d7b88a..909c264 100644
--- a/src/libvirt-domain.c
+++ b/src/libvirt-domain.c
@@ -6065,7 +6065,7 @@ virDomainBlockPeek(virDomainPtr dom,
     conn = dom->conn;

     virCheckReadOnlyGoto(conn->flags, error);
-    virCheckNonNullArgGoto(disk, error);
+    virCheckNonEmptyStringArgGoto(disk, error);

     /* Allow size == 0 as an access test. */
     if (size > 0)
@@ -6333,7 +6333,7 @@ virDomainGetBlockInfo(virDomainPtr domain, const char *disk,
         memset(info, 0, sizeof(*info));

     virCheckDomainReturn(domain, -1);
-    virCheckNonNullArgGoto(disk, error);
+    virCheckNonEmptyStringArgGoto(disk, error);
     virCheckNonNullArgGoto(info, error);

     conn = domain->conn;
diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index c1373de..004da7e 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -11530,12 +11530,6 @@ qemuDomainBlockPeek(virDomainPtr dom,
     if (virDomainBlockPeekEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;

-    if (!path || path[0] == '\0') {
-        virReportError(VIR_ERR_INVALID_ARG,
-                       "%s", _("NULL or empty path"));
-        goto cleanup;
-    }
-
     /* Check the path belongs to this domain.  */
     if (!(actual = virDomainDiskPathByName(vm->def, path))) {
         virReportError(VIR_ERR_INVALID_ARG,
@@ -11821,11 +11815,6 @@ qemuDomainGetBlockInfo(virDomainPtr dom,
     if (virDomainGetBlockInfoEnsureACL(dom->conn, vm->def) < 0)
         goto cleanup;

-    if (!path || path[0] == '\0') {
-        virReportError(VIR_ERR_INVALID_ARG, "%s", _("NULL or empty path"));
-        goto cleanup;
-    }
-
     /* Technically, we only need a job if we are going to query the
      * monitor, which is only for active domains that are using
      * non-raw block devices.  But it is easier to share code if we
diff --git a/src/util/virerror.h b/src/util/virerror.h
index ad3a946..c1a445e 100644
--- a/src/util/virerror.h
+++ b/src/util/virerror.h
@@ -95,6 +95,17 @@ void virReportSystemErrorFull(int domcode,
                       0, 0,                                          \
                       _("%s in %s must not be NULL"),                \
                       #argname, __FUNCTION__)
+# define virReportInvalidEmptyStringArg(argname)                     \
+    virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__,              \
+                      VIR_FROM_THIS,                                 \
+                      VIR_ERR_INVALID_ARG,                           \
+                      VIR_ERR_ERROR,                                 \
+                      __FUNCTION__,                                  \
+                      #argname,                                      \
+                      NULL,                                          \
+                      0, 0,                                          \
+                      _("string %s in %s must not be non empty"),    \
+                      #argname, __FUNCTION__)
 # define virReportInvalidPositiveArg(argname)                        \
     virRaiseErrorFull(__FILE__, __FUNCTION__, __LINE__,              \
                       VIR_FROM_THIS,                                 \
-- 
2.4.1




More information about the libvir-list mailing list