mirror of
https://gitlab.com/libvirt/libvirt.git
synced 2025-01-03 11:35:19 +00:00
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:
parent
0d42fa688b
commit
29592788f1
@ -346,8 +346,8 @@ 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))) {
|
if ((ret = virRandomBytes(buf, nbytes)) < 0) {
|
||||||
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;
|
||||||
}
|
}
|
||||||
|
@ -168,7 +168,7 @@ 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
|
* Returns 0 on success or -errno on failure
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
virRandomBytes(unsigned char *buf,
|
virRandomBytes(unsigned char *buf,
|
||||||
@ -177,7 +177,7 @@ virRandomBytes(unsigned char *buf,
|
|||||||
int fd;
|
int fd;
|
||||||
|
|
||||||
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
|
||||||
return errno;
|
return -errno;
|
||||||
|
|
||||||
while (buflen > 0) {
|
while (buflen > 0) {
|
||||||
ssize_t n;
|
ssize_t n;
|
||||||
@ -186,7 +186,7 @@ virRandomBytes(unsigned char *buf,
|
|||||||
if (errno == EINTR)
|
if (errno == EINTR)
|
||||||
continue;
|
continue;
|
||||||
VIR_FORCE_CLOSE(fd);
|
VIR_FORCE_CLOSE(fd);
|
||||||
return n < 0 ? errno : ENODATA;
|
return n < 0 ? -errno : -ENODATA;
|
||||||
}
|
}
|
||||||
|
|
||||||
buf += n;
|
buf += n;
|
||||||
|
@ -76,11 +76,11 @@ virUUIDGenerate(unsigned char *uuid)
|
|||||||
if (uuid == NULL)
|
if (uuid == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN))) {
|
if ((err = virRandomBytes(uuid, VIR_UUID_BUFLEN)) < 0) {
|
||||||
char ebuf[1024];
|
char ebuf[1024];
|
||||||
VIR_WARN("Falling back to pseudorandom UUID,"
|
VIR_WARN("Falling back to pseudorandom UUID,"
|
||||||
" failed to generate random bytes: %s",
|
" failed to generate random bytes: %s",
|
||||||
virStrerror(err, ebuf, sizeof(ebuf)));
|
virStrerror(-err, ebuf, sizeof(ebuf)));
|
||||||
err = virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
|
err = virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -88,8 +88,8 @@ testCryptoEncrypt(const void *opaque)
|
|||||||
VIR_ALLOC_N(iv, ivlen) < 0)
|
VIR_ALLOC_N(iv, ivlen) < 0)
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
|
|
||||||
if (virRandomBytes(enckey, enckeylen) ||
|
if (virRandomBytes(enckey, enckeylen) < 0 ||
|
||||||
virRandomBytes(iv, ivlen)) {
|
virRandomBytes(iv, ivlen) < 0) {
|
||||||
fprintf(stderr, "Failed to generate random bytes\n");
|
fprintf(stderr, "Failed to generate random bytes\n");
|
||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user