]> xenbits.xensource.com Git - people/liuw/libxenctrl-split/libvirt.git/commitdiff
Introduce virBufferCheckError
authorJán Tomko <jtomko@redhat.com>
Fri, 27 Jun 2014 07:23:13 +0000 (09:23 +0200)
committerJán Tomko <jtomko@redhat.com>
Thu, 3 Jul 2014 08:41:15 +0000 (10:41 +0200)
Check if the buffer is in error state and report an error if it is.

This replaces the pattern:
if (virBufferError(buf)) {
    virReportOOMError();
    goto cleanup;
}
with:

if (virBufferCheckError(buf) < 0)
    goto cleanup;

Document typical buffer usage to favor this.
Also remove the redundant FreeAndReset - if an error has
been set via virBufferSetError, the content is already freed.

HACKING
docs/hacking.html.in
po/POTFILES.in
src/libvirt_private.syms
src/util/virbuffer.c
src/util/virbuffer.h

diff --git a/HACKING b/HACKING
index ac3e9d7f9737915f77caf6f0f6abb46e81ad2f6d..0745d5f3372ae145df1d14c365f6e95c1e74ee19 100644 (file)
--- a/HACKING
+++ b/HACKING
@@ -776,7 +776,7 @@ Variable length string buffer
 =============================
 If there is a need for complex string concatenations, avoid using the usual
 sequence of malloc/strcpy/strcat/snprintf functions and make use of the
-virBuffer API described in buf.h
+virBuffer API described in virbuffer.h
 
 Typical usage is as follows:
 
@@ -794,11 +794,8 @@ Typical usage is as follows:
 
      ...
 
-     if (virBufferError(&buf)) {
-         virBufferFreeAndReset(&buf);
-         virReportOOMError();
+     if (virBufferCheckError(&buf) < 0)
          return NULL;
-     }
 
      return virBufferContentAndReset(&buf);
   }
index 9c6dd2682020736d01e41eee1269f37d2b79ee2d..9456520d4600209326e70ee055b32c584466358b 100644 (file)
     <p>
       If there is a need for complex string concatenations, avoid using
       the usual sequence of malloc/strcpy/strcat/snprintf functions and
-      make use of the virBuffer API described in buf.h
+      make use of the virBuffer API described in virbuffer.h
     </p>
 
     <p>Typical usage is as follows:</p>
 
      ...
 
-     if (virBufferError(&amp;buf)) {
-         virBufferFreeAndReset(&amp;buf);
-         virReportOOMError();
+     if (virBufferCheckError(&amp;buf) &lt; 0)
          return NULL;
-     }
 
      return virBufferContentAndReset(&amp;buf);
   }
index 31a8381748cffbff5d41683a15cad21e7759fdae..d338151cae3236607b3df704ba0f0001fdd14fa4 100644 (file)
@@ -156,6 +156,7 @@ src/util/viraudit.c
 src/util/virauth.c
 src/util/virauthconfig.c
 src/util/virbitmap.c
+src/util/virbuffer.c
 src/util/vircgroup.c
 src/util/virclosecallbacks.c
 src/util/vircommand.c
index ed56103a17870226ae1196ac31aa390aeb2eff8c..cc5fd325af7bff8a2fae2ea65d577a7cddc0eaad 100644 (file)
@@ -1008,6 +1008,7 @@ virBufferAdd;
 virBufferAddChar;
 virBufferAdjustIndent;
 virBufferAsprintf;
+virBufferCheckErrorInternal;
 virBufferContentAndReset;
 virBufferCurrentContent;
 virBufferError;
index eb29012ec88562e1e3463741961aff538a404527..025f0ab7d2710a820625ab8b7fe47b9d57d43927 100644 (file)
@@ -32,6 +32,7 @@
 
 #include "virbuffer.h"
 #include "viralloc.h"
+#include "virerror.h"
 
 
 /* If adding more fields, ensure to edit buf.h to match
@@ -263,6 +264,36 @@ virBufferError(const virBuffer *buf)
     return buf->error;
 }
 
+/**
+ * virBufferCheckErrorInternal:
+ * @buf: the buffer
+ *
+ * Report an error if the buffer is in an error state.
+ *
+ * Return -1 if an error has been reported, 0 otherwise.
+ */
+int
+virBufferCheckErrorInternal(const virBuffer *buf,
+                            int domcode,
+                            const char *filename,
+                            const char *funcname,
+                            size_t linenr)
+{
+    if (buf->error == 0)
+        return 0;
+
+    if (buf->error == ENOMEM) {
+        virReportOOMErrorFull(domcode, filename, funcname, linenr);
+        errno = ENOMEM;
+    } else {
+        virReportErrorHelper(domcode, VIR_ERR_INTERNAL_ERROR, filename,
+                             funcname, linenr, "%s",
+                             _("Invalid buffer API usage"));
+        errno = EINVAL;
+    }
+    return -1;
+}
+
 /**
  * virBufferUse:
  * @buf: the usage of the string in the buffer
index a0cc4e67771f5769bcfd5781f3dea52d02414bcd..bdfff5e817d6aff1b17003d19cbcd33955cea029 100644 (file)
@@ -53,6 +53,23 @@ const char *virBufferCurrentContent(virBufferPtr buf);
 char *virBufferContentAndReset(virBufferPtr buf);
 void virBufferFreeAndReset(virBufferPtr buf);
 int virBufferError(const virBuffer *buf);
+int virBufferCheckErrorInternal(const virBuffer *buf,
+                                int domcode,
+                                const char *filename,
+                                const char *funcname,
+                                size_t linenr)
+    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1);
+/**
+ * virBufferCheckError
+ *
+ * Checks if the buffer is in error state and reports an error.
+ *
+ * Returns 0 if no error has occured, otherwise an error is reported
+ * and -1 is returned.
+ */
+# define virBufferCheckError(buf) \
+    virBufferCheckErrorInternal(buf, VIR_FROM_THIS, __FILE__, __FUNCTION__, \
+    __LINE__)
 unsigned int virBufferUse(const virBuffer *buf);
 void virBufferAdd(virBufferPtr buf, const char *str, int len);
 void virBufferAddChar(virBufferPtr buf, char c);