[libvirt] [PATCH] util: Perform proper virRandomBytes return value checking

John Ferlan jferlan at redhat.com
Tue Jun 7 11:37:22 UTC 2016


Document the return value of virRandomBytes as 0 or some errno value and
then make sure all callers make the proper checks.

Signed-off-by: John Ferlan <jferlan at redhat.com>
---
 src/util/vircrypto.c  | 2 +-
 src/util/virrandom.c  | 2 ++
 tests/vircryptotest.c | 6 ++++--
 tests/virrandomtest.c | 2 +-
 4 files changed, 8 insertions(+), 4 deletions(-)

diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c
index 4f288f0..8452936 100644
--- a/src/util/vircrypto.c
+++ b/src/util/vircrypto.c
@@ -301,7 +301,7 @@ virCryptoGenerateRandom(size_t nbytes)
     /* If we don't have gnutls_rnd(), we will generate a less cryptographically
      * strong master buf from /dev/urandom.
      */
-    if ((ret = virRandomBytes(buf, nbytes)) < 0) {
+    if ((ret = virRandomBytes(buf, nbytes))) {
         virReportSystemError(ret, "%s", _("failed to generate byte stream"));
         VIR_FREE(buf);
         return NULL;
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 62a0e31..41daa40 100644
--- a/src/util/virrandom.c
+++ b/src/util/virrandom.c
@@ -167,6 +167,8 @@ uint32_t virRandomInt(uint32_t max)
  *
  * Generate a stream of random bytes from /dev/urandom
  * into @buf of size @buflen
+ *
+ * Returns 0 on success or an errno on failure
  */
 int
 virRandomBytes(unsigned char *buf,
diff --git a/tests/vircryptotest.c b/tests/vircryptotest.c
index 72265d9..e7b7de5 100644
--- a/tests/vircryptotest.c
+++ b/tests/vircryptotest.c
@@ -87,9 +87,11 @@ testCryptoEncrypt(const void *opaque)
         VIR_ALLOC_N(iv, ivlen) < 0)
         goto cleanup;
 
-    if (virRandomBytes(enckey, enckeylen) < 0 ||
-        virRandomBytes(iv, ivlen) < 0)
+    if (virRandomBytes(enckey, enckeylen) ||
+        virRandomBytes(iv, ivlen)) {
+        fprintf(stderr, "Failed to generate random bytes\n");
         goto cleanup;
+    }
 
     if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen,
                              data->input, data->inputlen,
diff --git a/tests/virrandomtest.c b/tests/virrandomtest.c
index 367bdc7..687ebd9 100644
--- a/tests/virrandomtest.c
+++ b/tests/virrandomtest.c
@@ -40,7 +40,7 @@ testRandomBytes(const void *unused ATTRIBUTE_UNUSED)
     if (VIR_ALLOC_N(data, datalen) < 0)
         return -1;
 
-    if (virRandomBytes(data, datalen) < 0) {
+    if (virRandomBytes(data, datalen)) {
         fprintf(stderr, "Failed to generate random bytes");
         goto cleanup;
     }
-- 
2.5.5




More information about the libvir-list mailing list