]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Fix virGetLastErrorMessage to return proper error when 'err' is NULL
authorErik Skultety <eskultet@redhat.com>
Wed, 11 May 2016 18:10:35 +0000 (20:10 +0200)
committerErik Skultety <eskultet@redhat.com>
Wed, 11 May 2016 18:10:35 +0000 (20:10 +0200)
Both virGetLastError and virGetLastErrorMessage call virLastErrorObject method
that returns a thread-local error object. However, if a direct call to malloc
or pthread_setspecific (probably also due to malloc, since it sets ENOMEM)
fail, virLastErrorObject returns NULL which, although incorrectly interpreted
by virGetLastError as no error, still requires the caller to check for NULL
pointer. This isn't the case with virGetLastErrorMessage that also treated it
incorrectly as no error, but returned the literal "no error".
This patch tweaks the checks in the virGetLastErrorMessage function, so that
if virLastErrorObject failed, it returned "unknown error" which is equivalent
to the current approach with virGetLastError and if it returned NULL,
"unknown error" was set.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
src/util/virerror.c

index 5d875e37766b0cb597472508d77c1ef1ba57c159..1177570ef0d540249302ca14cb5ea00d0681c962 100644 (file)
@@ -281,9 +281,9 @@ const char *
 virGetLastErrorMessage(void)
 {
     virErrorPtr err = virLastErrorObject();
-    if (!err || err->code == VIR_ERR_OK)
+    if (err && err->code == VIR_ERR_OK)
         return _("no error");
-    if (err->message == NULL)
+    if (!err || !err->message)
         return _("unknown error");
     return err->message;
 }