[PATCH 3/5] virstring: Introduce virVasprintfNOOM and make virVasprintf report OOM

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

 



---
 src/libvirt_private.syms |  1 +
 src/qemu/qemu_domain.c   |  4 +---
 src/util/viraudit.c      |  2 +-
 src/util/vircommand.c    |  4 ++--
 src/util/virerror.c      |  2 +-
 src/util/virlog.c        |  2 +-
 src/util/virstring.c     | 22 ++++++++++++++++++++--
 src/util/virstring.h     |  3 +++
 8 files changed, 30 insertions(+), 10 deletions(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 5060b87..8d1abe7 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1742,6 +1742,7 @@ virStrToLong_ul;
 virStrToLong_ull;
 virTrimSpaces;
 virVasprintf;
+virVasprintfNOOM;
 
 
 # util/virsysinfo.h
diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c
index ad8b811..78b4dd7 100644
--- a/src/qemu/qemu_domain.c
+++ b/src/qemu/qemu_domain.c
@@ -1477,10 +1477,8 @@ int qemuDomainAppendLog(virQEMUDriverPtr driver,
         (fd = qemuDomainCreateLog(driver, obj, true)) < 0)
         goto cleanup;
 
-    if (virVasprintf(&message, fmt, argptr) < 0) {
-        virReportOOMError();
+    if (virVasprintf(&message, fmt, argptr) < 0)
         goto cleanup;
-    }
     if (safewrite(fd, message, strlen(message)) < 0) {
         virReportSystemError(errno, _("Unable to write to domain logfile %s"),
                              obj->def->name);
diff --git a/src/util/viraudit.c b/src/util/viraudit.c
index 2588b21..7640357 100644
--- a/src/util/viraudit.c
+++ b/src/util/viraudit.c
@@ -96,7 +96,7 @@ void virAuditSend(const char *filename,
 #endif
 
     va_start(args, fmt);
-    if (virVasprintf(&str, fmt, args) < 0) {
+    if (virVasprintfNOOM(&str, fmt, args) < 0) {
         VIR_WARN("Out of memory while formatting audit message");
         str = NULL;
     }
diff --git a/src/util/vircommand.c b/src/util/vircommand.c
index 789d62d..c653f7b 100644
--- a/src/util/vircommand.c
+++ b/src/util/vircommand.c
@@ -1125,7 +1125,7 @@ virCommandAddEnvFormat(virCommandPtr cmd, const char *format, ...)
         return;
 
     va_start(list, format);
-    if (virVasprintf(&env, format, list) < 0) {
+    if (virVasprintfNOOM(&env, format, list) < 0) {
         cmd->has_error = ENOMEM;
         va_end(list);
         return;
@@ -1340,7 +1340,7 @@ virCommandAddArgFormat(virCommandPtr cmd, const char *format, ...)
         return;
 
     va_start(list, format);
-    if (virVasprintf(&arg, format, list) < 0) {
+    if (virVasprintfNOOM(&arg, format, list) < 0) {
         cmd->has_error = ENOMEM;
         va_end(list);
         return;
diff --git a/src/util/virerror.c b/src/util/virerror.c
index afb3c7a..d89f505 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -649,7 +649,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
     } else {
         va_list ap;
         va_start(ap, fmt);
-        ignore_value(virVasprintf(&str, fmt, ap));
+        ignore_value(virVasprintfNOOM(&str, fmt, ap));
         va_end(ap);
     }
 
diff --git a/src/util/virlog.c b/src/util/virlog.c
index 7fb7896..ebe9762 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -830,7 +830,7 @@ virLogVMessage(virLogSource source,
     /*
      * serialize the error message, add level and timestamp
      */
-    if (virVasprintf(&str, fmt, vargs) < 0) {
+    if (virVasprintfNOOM(&str, fmt, vargs) < 0) {
         goto cleanup;
     }
 
diff --git a/src/util/virstring.c b/src/util/virstring.c
index 3847811..de7035f 100644
--- a/src/util/virstring.c
+++ b/src/util/virstring.c
@@ -316,7 +316,7 @@ virStrToDouble(char const *s,
  * like glibc's vasprintf but makes sure *strp == NULL on failure
  */
 int
-virVasprintf(char **strp, const char *fmt, va_list list)
+virVasprintfNOOM(char **strp, const char *fmt, va_list list)
 {
     int ret;
 
@@ -327,6 +327,23 @@ virVasprintf(char **strp, const char *fmt, va_list list)
 }
 
 /**
+ * virVasprintf
+ *
+ * like glibc's vasprintf but makes sure *strp == NULL on failure and reports
+ * OOM error.
+ */
+int
+virVasprintf(char **strp, const char *fmt, va_list list)
+{
+    int ret;
+
+    if ((ret = virVasprintfNOOM(strp, fmt, list)) == -1)
+        virReportOOMError();
+
+    return ret;
+}
+
+/**
  * virAsprintf
  *
  * like glibc's_asprintf but makes sure *strp == NULL on failure
@@ -338,7 +355,8 @@ virAsprintf(char **strp, const char *fmt, ...)
     int ret;
 
     va_start(ap, fmt);
-    ret = virVasprintf(strp, fmt, ap);
+    /* XXX Don't report OOM error for now, unless all code is adapted. */
+    ret = virVasprintfNOOM(strp, fmt, ap);
     va_end(ap);
     return ret;
 }
diff --git a/src/util/virstring.h b/src/util/virstring.h
index 889951c..9c2518c 100644
--- a/src/util/virstring.h
+++ b/src/util/virstring.h
@@ -65,6 +65,9 @@ int virStrToDouble(char const *s,
                    char **end_ptr,
                    double *result);
 
+int virVasprintfNOOM(char **strp, const char *fmt, va_list list)
+    ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0)
+    ATTRIBUTE_RETURN_CHECK;
 int virVasprintf(char **strp, const char *fmt, va_list list)
     ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(2) ATTRIBUTE_FMT_PRINTF(2, 0)
     ATTRIBUTE_RETURN_CHECK;
-- 
1.8.1.5

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]