[libvirt] [PATCH] fix errors in virReportSystemErrorFull

Jim Meyering jim at meyering.net
Mon Jan 26 14:02:48 UTC 2009


While looking at removing most of the remaining uses of strerror,
I spotted problems in virReportSystemErrorFull.
  1) it would leak "combined"
  2) that allocated, written-into buffer wasn't even used

So, since we'd rather avoid having to allocate at all in the
error-reporting path (esp for OOM errors), I've made it automatic.

>From 8550ec841b4e19049abc4b5e4a2f8a81d1824055 Mon Sep 17 00:00:00 2001
From: Jim Meyering <meyering at redhat.com>
Date: Mon, 26 Jan 2009 14:59:35 +0100
Subject: [PATCH] fix errors in virReportSystemErrorFull

* src/virterror.c (virReportSystemErrorFull): Don't leak "combined".
In fact, don't even attempt allocation.
Do include the result of formatted print in final diagnostic.
---
 src/virterror.c |   13 +++++++++----
 1 files changed, 9 insertions(+), 4 deletions(-)

diff --git a/src/virterror.c b/src/virterror.c
index 0c66781..cfde1dc 100644
--- a/src/virterror.c
+++ b/src/virterror.c
@@ -1019,7 +1019,7 @@ void virReportSystemErrorFull(virConnectPtr conn,
     char systemError[1024];
     char *theerrnostr;
     const char *virerr;
-    char *combined = NULL;
+    char combined[2048];

 #ifdef HAVE_STRERROR_R
 #ifdef __USE_GNU
@@ -1047,10 +1047,15 @@ void virReportSystemErrorFull(virConnectPtr conn,
         errorMessage[0] = '\0';
     }

-    if (virAsprintf(&combined, "%s: %s", errorMessage, theerrnostr) < 0)
-        combined = theerrnostr; /* OOM, so lets just pass the strerror info as best effort */
+    char *err_str;
+    int n = snprintf(combined, sizeof combined, "%s: %s",
+                     errorMessage, theerrnostr);
+    err_str = (0 < n && n < sizeof(combined)
+               ? combined
+               /* use the strerror info as best effort */
+               : theerrnostr);

-    virerr = virErrorMsg(VIR_ERR_SYSTEM_ERROR, (errorMessage[0] ? errorMessage : NULL));
+    virerr = virErrorMsg(VIR_ERR_SYSTEM_ERROR, (err_str[0] ? err_str : NULL));
     virRaiseError(conn, NULL, NULL, domcode, VIR_ERR_SYSTEM_ERROR, VIR_ERR_ERROR,
                   virerr, errorMessage, NULL, -1, -1, virerr, errorMessage);
 }
--
1.6.1.1.347.g3f81d




More information about the libvir-list mailing list