From nobody Wed May 14 20:49:18 2025 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=pass(p=none dis=none) header.from=redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1527582318308862.926024961777; Tue, 29 May 2018 01:25:18 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id A525E30C7458; Tue, 29 May 2018 08:25:15 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 165A91001918; Tue, 29 May 2018 08:25:15 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 715614CA82; Tue, 29 May 2018 08:25:13 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w4T8PC8Z005759 for ; Tue, 29 May 2018 04:25:12 -0400 Received: by smtp.corp.redhat.com (Postfix) id 63276422BA; Tue, 29 May 2018 08:25:12 +0000 (UTC) Received: from moe.brq.redhat.com (unknown [10.43.2.192]) by smtp.corp.redhat.com (Postfix) with ESMTP id DA06763F7E; Tue, 29 May 2018 08:25:11 +0000 (UTC) From: Michal Privoznik To: libvir-list@redhat.com Date: Tue, 29 May 2018 10:24:37 +0200 Message-Id: <0ce0ff54d9cf72e76878e3fad46285e2d8d47d64.1527581861.git.mprivozn@redhat.com> In-Reply-To: References: In-Reply-To: References: X-Scanned-By: MIMEDefang 2.79 on 10.11.54.5 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 01/10] virRandomBytes: Fix return value X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.84 on 10.5.11.22 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.44]); Tue, 29 May 2018 08:25:16 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" 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 Reviewed-by: Eric Blake --- src/util/vircrypto.c | 4 ++-- src/util/virrandom.c | 6 +++--- src/util/viruuid.c | 4 ++-- tests/vircryptotest.c | 4 ++-- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/src/util/vircrypto.c b/src/util/vircrypto.c index bbc2a01f22..4079013d6d 100644 --- a/src/util/vircrypto.c +++ b/src/util/vircrypto.c @@ -346,8 +346,8 @@ virCryptoGenerateRandom(size_t nbytes) /* If we don't have gnutls_rnd(), we will generate a less cryptographi= cally * strong master buf from /dev/urandom. */ - if ((ret =3D virRandomBytes(buf, nbytes))) { - virReportSystemError(ret, "%s", _("failed to generate byte stream"= )); + if ((ret =3D virRandomBytes(buf, nbytes)) < 0) { + virReportSystemError(-ret, "%s", _("failed to generate byte stream= ")); VIR_FREE(buf); return NULL; } diff --git a/src/util/virrandom.c b/src/util/virrandom.c index 41daa404b2..9597640840 100644 --- a/src/util/virrandom.c +++ b/src/util/virrandom.c @@ -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 an -errno on failure */ int virRandomBytes(unsigned char *buf, @@ -177,7 +177,7 @@ virRandomBytes(unsigned char *buf, int fd; =20 if ((fd =3D open("/dev/urandom", O_RDONLY)) < 0) - return errno; + return -errno; =20 while (buflen > 0) { ssize_t n; @@ -186,7 +186,7 @@ virRandomBytes(unsigned char *buf, if (errno =3D=3D EINTR) continue; VIR_FORCE_CLOSE(fd); - return n < 0 ? errno : ENODATA; + return n < 0 ? -errno : -ENODATA; } =20 buf +=3D n; diff --git a/src/util/viruuid.c b/src/util/viruuid.c index 3cbaae0b85..61877aeba4 100644 --- a/src/util/viruuid.c +++ b/src/util/viruuid.c @@ -76,11 +76,11 @@ virUUIDGenerate(unsigned char *uuid) if (uuid =3D=3D NULL) return -1; =20 - if ((err =3D virRandomBytes(uuid, VIR_UUID_BUFLEN))) { + if ((err =3D 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 =3D virUUIDGeneratePseudoRandomBytes(uuid, VIR_UUID_BUFLEN); } =20 diff --git a/tests/vircryptotest.c b/tests/vircryptotest.c index d9ffc6f34c..b6313e73ad 100644 --- a/tests/vircryptotest.c +++ b/tests/vircryptotest.c @@ -88,8 +88,8 @@ testCryptoEncrypt(const void *opaque) VIR_ALLOC_N(iv, ivlen) < 0) goto cleanup; =20 - 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; } --=20 2.16.1 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list