[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCHv2 03/13] virbuf: more detailed error reporting



The next patch wants to add some sanity checking, which would
be a different error than ENOMEM.  Many existing callers blindly
report OOM failure if virBuf reports an error, and this will be
wrong in the (unlikely) case that they actually had a usage error
instead; but since the most common error really is ENOMEM, I'm
not going to fix all callers.  Meanwhile, new discriminating
callers can react differently depending on what failure happened.

* src/util/buf.c (virBufferSetError): Add parameter.
(virBufferGrow, virBufferVasprintf, virBufferEscapeString)
(virBufferEscapeSexpr): Adjust callers.
---
 src/util/buf.c |   26 +++++++++++++-------------
 1 files changed, 13 insertions(+), 13 deletions(-)

diff --git a/src/util/buf.c b/src/util/buf.c
index c737696..98bb681 100644
--- a/src/util/buf.c
+++ b/src/util/buf.c
@@ -27,24 +27,24 @@
 struct _virBuffer {
     unsigned int size;
     unsigned int use;
-    unsigned int error;
+    unsigned int error; /* errno value, or -1 for usage error */
     char *content;
 };

 /**
  * virBufferFail
  * @buf: the buffer
+ * @error: which error occurred (errno value, or -1 for usage)
  *
- * Mark the buffer has having failed a memory allocation,
- * freeing the content and setting the error flag.
+ * Mark the buffer as failed, free the content and set the error flag.
  */
 static void
-virBufferSetError(virBufferPtr buf)
+virBufferSetError(virBufferPtr buf, int error)
 {
     VIR_FREE(buf->content);
     buf->size = 0;
     buf->use = 0;
-    buf->error = 1;
+    buf->error = error;
 }

 /**
@@ -70,7 +70,7 @@ virBufferGrow(virBufferPtr buf, unsigned int len)
     size = buf->use + len + 1000;

     if (VIR_REALLOC_N(buf->content, size) < 0) {
-        virBufferSetError(buf);
+        virBufferSetError(buf, errno);
         return -1;
     }
     buf->size = size;
@@ -184,15 +184,15 @@ void virBufferFreeAndReset(virBufferPtr buf)
  * @buf: the buffer
  *
  * Check to see if the buffer is in an error state due
- * to failed memory allocation
+ * to failed memory allocation or usage error
  *
- * Return true if in error, 0 if normal
+ * Return positive errno value or -1 on usage error, 0 if normal
  */
 int
 virBufferError(const virBufferPtr buf)
 {
     if (buf == NULL)
-        return 1;
+        return -1;

     return buf->error;
 }
@@ -258,7 +258,7 @@ virBufferVasprintf(virBufferPtr buf, const char *format, va_list argptr)
     size = buf->size - buf->use;
     if ((count = vsnprintf(&buf->content[buf->use],
                            size, format, copy)) < 0) {
-        virBufferSetError(buf);
+        virBufferSetError(buf, errno);
         va_end(copy);
         return;
     }
@@ -276,7 +276,7 @@ virBufferVasprintf(virBufferPtr buf, const char *format, va_list argptr)
         size = buf->size - buf->use;
         if ((count = vsnprintf(&buf->content[buf->use],
                                size, format, argptr)) < 0) {
-            virBufferSetError(buf);
+            virBufferSetError(buf, errno);
             return;
         }
     }
@@ -313,7 +313,7 @@ virBufferEscapeString(virBufferPtr buf, const char *format, const char *str)

     if (xalloc_oversized(6, len) ||
         VIR_ALLOC_N(escaped, 6 * len + 1) < 0) {
-        virBufferSetError(buf);
+        virBufferSetError(buf, errno);
         return;
     }

@@ -401,7 +401,7 @@ virBufferEscapeSexpr(virBufferPtr buf,

     if (xalloc_oversized(2, len) ||
         VIR_ALLOC_N(escaped, 2 * len + 1) < 0) {
-        virBufferSetError(buf);
+        virBufferSetError(buf, errno);
         return;
     }

-- 
1.7.4.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]