mirror of https://gitee.com/openkylin/libvirt.git
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:
parent
cf922bf837
commit
456ccc14d5
|
@ -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;
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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,
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue