util: Perform proper virRandomBytes return value checking

Document the return value of virRandomBytes as 0 or some errno value and
then make sure all callers make the proper checks.
This commit is contained in:
John Ferlan 2016-06-07 07:24:31 -04:00
parent cf922bf837
commit 456ccc14d5
4 changed files with 8 additions and 4 deletions

View File

@ -315,7 +315,7 @@ virCryptoGenerateRandom(size_t nbytes)
/* If we don't have gnutls_rnd(), we will generate a less cryptographically /* If we don't have gnutls_rnd(), we will generate a less cryptographically
* strong master buf from /dev/urandom. * 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")); virReportSystemError(ret, "%s", _("failed to generate byte stream"));
VIR_FREE(buf); VIR_FREE(buf);
return NULL; return NULL;

View File

@ -167,6 +167,8 @@ uint32_t virRandomInt(uint32_t max)
* *
* Generate a stream of random bytes from /dev/urandom * Generate a stream of random bytes from /dev/urandom
* into @buf of size @buflen * into @buf of size @buflen
*
* Returns 0 on success or an errno on failure
*/ */
int int
virRandomBytes(unsigned char *buf, virRandomBytes(unsigned char *buf,

View File

@ -87,9 +87,11 @@ testCryptoEncrypt(const void *opaque)
VIR_ALLOC_N(iv, ivlen) < 0) VIR_ALLOC_N(iv, ivlen) < 0)
goto cleanup; goto cleanup;
if (virRandomBytes(enckey, enckeylen) < 0 || if (virRandomBytes(enckey, enckeylen) ||
virRandomBytes(iv, ivlen) < 0) virRandomBytes(iv, ivlen)) {
fprintf(stderr, "Failed to generate random bytes\n");
goto cleanup; goto cleanup;
}
if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen, if (virCryptoEncryptData(data->algorithm, enckey, enckeylen, iv, ivlen,
data->input, data->inputlen, data->input, data->inputlen,

View File

@ -40,7 +40,7 @@ testRandomBytes(const void *unused ATTRIBUTE_UNUSED)
if (VIR_ALLOC_N(data, datalen) < 0) if (VIR_ALLOC_N(data, datalen) < 0)
return -1; return -1;
if (virRandomBytes(data, datalen) < 0) { if (virRandomBytes(data, datalen)) {
fprintf(stderr, "Failed to generate random bytes"); fprintf(stderr, "Failed to generate random bytes");
goto cleanup; goto cleanup;
} }