[libvirt] [PATCH] Ensure virStrerror always sets an error string

Daniel P. Berrange berrange at redhat.com
Wed May 18 17:07:44 UTC 2011


strerror_r() is free to not set any error string, if the passed
errno is not valid. It may, however, still return a pointer to
the original passed in buffer. This resulting in random garbage
from the stack being present as the error string.

To reliably detect case where no error string is set, pre-init
the buffer to all-zeros, and then check for empty string after
calling sterror_r

* src/util/virterror.c: Ensure virStrerror always sets an
  error string
---
 src/util/virterror.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)

diff --git a/src/util/virterror.c b/src/util/virterror.c
index 2d7309a..eff8468 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -1267,9 +1267,13 @@ const char *virStrerror(int theerrno, char *errBuf, size_t errBufLen)
     int save_errno = errno;
     const char *ret;
 
+    memset(errBuf, 0, errBufLen);
     strerror_r(theerrno, errBuf, errBufLen);
     ret = errBuf;
     errno = save_errno;
+
+    if (ret[0] == '\0')
+        strncpy(errBuf, _("Unknown errno"), errBufLen);
     return ret;
 }
 
-- 
1.7.4.4




More information about the libvir-list mailing list