]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Perform proper virRandomBytes return value checking
authorJohn Ferlan <jferlan@redhat.com>
Tue, 7 Jun 2016 11:24:31 +0000 (07:24 -0400)
committerJohn Ferlan <jferlan@redhat.com>
Tue, 7 Jun 2016 14:18:36 +0000 (10:18 -0400)
Document the return value of virRandomBytes as 0 or some errno value and
then make sure all callers make the proper checks.

src/util/vircrypto.c
src/util/virrandom.c
tests/vircryptotest.c
tests/virrandomtest.c

index aaede94c6394af4479c7ac0f41d07fbddeb994ea..03410a1a44013887fb8103c74a1aa90d33fee0d9 100644 (file)
@@ -315,7 +315,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;
index 62a0e314e664ec2753644c807294e6c2f0e10737..41daa404b27350ae0a9f49f1dea0084d7e4c0fa9 100644 (file)
@@ -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,
index 72265d949a44a6ee7f25456d6213a2cda7e52945..e7b7de513135781176c70c856779fe445479c651 100644 (file)
@@ -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,
index 367bdc70548e04c4aee7caa8997f9cd2a030df0f..687ebd9bf4c3d5e150d205b72212b895d3a066d3 100644 (file)
@@ -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;
     }