virRandomBytes: Fix return value

In libvirt when a function wants to return an error code it
should be a negative value. Returning a positive value (or zero)
means success. But virRandomBytes() does not follow this rule.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Eric Blake <eblake@redhat.com>
This commit is contained in:
Michal Privoznik 2018-05-29 09:02:57 +02:00
parent 0d42fa688b
commit 29592788f1
4 changed files with 9 additions and 9 deletions

View File

@ -346,8 +346,8 @@ 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))) {
virReportSystemError(ret, "%s", _("failed to generate byte stream"));
if ((ret = virRandomBytes(buf, nbytes)) < 0) {
virReportSystemError(-ret, "%s", _("failed to generate byte stream"));
VIR_FREE(buf);
return NULL;
}

View File

@ -168,7 +168,7 @@ 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
* Returns 0 on success or -errno on failure
*/
int
virRandomBytes(unsigned char *buf,
@ -177,7 +177,7 @@ virRandomBytes(unsigned char *buf,
int fd;
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
return errno;
return -errno;
while (buflen > 0) {
ssize_t n;
@ -186,7 +186,7 @@ virRandomBytes(unsigned char *buf,
if (errno == EINTR)
continue;
VIR_FORCE_CLOSE(fd);
return n < 0 ? errno : ENODATA;
return n < 0 ? -errno : -ENODATA;
}
buf += n;

View File

@ -76,11 +76,11 @@ virUUIDGenerate(unsigned char *uuid)
if (uuid == NULL)
return -1;
if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN))) {
if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN)) < 0) {
char ebuf[1024];
VIR_WARN("Falling back to pseudorandom UUID,"
" failed to generate random bytes: %s",
virStrerror(err, ebuf, sizeof(ebuf)));
virStrerror(-err, ebuf, sizeof(ebuf)));
err = virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
}

View File

@ -88,8 +88,8 @@ testCryptoEncrypt(const void *opaque)
VIR_ALLOC_N(iv, ivlen) < 0)
goto cleanup;
if (virRandomBytes(enckey, enckeylen) ||
virRandomBytes(iv, ivlen)) {
if (virRandomBytes(enckey, enckeylen) < 0 ||
virRandomBytes(iv, ivlen) < 0) {
fprintf(stderr, "Failed to generate random bytes\n");
goto cleanup;
}